Following on my previous question: How to parse JSON with XE2 dbxJSON
Where I had a clear answer on how to get a value out of a JSON string. I am now requiring to get the value from an array.
The full JSON string:
{"ATTITUDE":{"message_information":{"counter":45942,"frequency":10.07058334350586,"time":{"first_message":"2021-06-02T04:17:19.713053488+00:00","last_message":"2021-06-02T05:33:21.925390505+00:00"}},"pitch":0.39779675006866455,"pitchspeed":0.04370392858982086,"roll":-3.1154606342315674,"rollspeed":0.29034918546676636,"time_boot_ms":4587189,"type":"ATTITUDE","yaw":2.762258768081665,"yawspeed":0.39911729097366333},"AUTOPILOT_VERSION":{"board_version":0,"capabilities":{"bits":10695},"flight_custom_version":[48,53,102,101,100,52,57,98],"flight_sw_version":50660607,"message_information":{"counter":1,"frequency":null,"time":{"first_message":"2021-06-02T05:03:34.953044355+00:00","last_message":"2021-06-02T05:03:34.953068261+00:00"}},"middleware_custom_version":[56,100,53,48,53,97,48,50],"middleware_sw_version":0,"os_custom_version":[49,97,57,57,98,97,53,56],"os_sw_version":0,"product_id":0,"type":"AUTOPILOT_VERSION","uid":0,"uid2":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"vendor_id":0},"BATTERY_STATUS":{"battery_function":{"type":"MAV_BATTERY_FUNCTION_UNKNOWN"},"battery_remaining":99,"charge_state":{"type":"MAV_BATTERY_CHARGE_STATE_UNDEFINED"},"current_battery":4,"current_consumed":51,"energy_consumed":-1,"id":0,"mavtype":{"type":"MAV_BATTERY_TYPE_UNKNOWN"},"message_information":{"counter":45443,"frequency":9.961201667785645,"time":{"first_message":"2021-06-02T04:17:19.729121346+00:00","last_message":"2021-06-02T05:33:21.936851877+00:00"}},"temperature":32767,"time_remaining":0,"type":"BATTERY_STATUS","voltages":[65535,65535,65535,65535,65535,65535,65535,65535,65535,65535]},"COMMAND_ACK":{"command":{"type":"MAV_CMD_REQUEST_PROTOCOL_VERSION"},"message_information":{"counter":2,"frequency":null,"time":{"first_message":"2021-06-02T05:03:34.954980156+00:00","last_message":"2021-06-02T05:03:35.688089082+00:00"}},"progress":0,"result":{"type":"MAV_RESULT_UNSUPPORTED"},"result_param2":0,"target_component":0,"target_system":0,"type":"COMMAND_ACK"},"GLOBAL_POSITION_INT":{"alt":0,"hdg":15816,"lat":0,"lon":0,"message_information":{"counter":45452,"frequency":9.963173866271973,"time":{"first_message":"2021-06-02T04:17:19.707524674+00:00","last_message":"2021-06-02T05:33:21.943859157+00:00"}},"relative_alt":0,"time_boot_ms":4587209,"type":"GLOBAL_POSITION_INT","vx":-10,"vy":11,"vz":0},"GPS_RAW_INT":{"alt":0,"alt_ellipsoid":0,"cog":0,"eph":9999,"epv":0,"fix_type":{"type":"GPS_FIX_TYPE_NO_GPS"},"h_acc":0,"hdg_acc":0,"lat":0,"lon":0,"message_information":{"counter":4532,"frequency":0.99364173412323,"time":{"first_message":"2021-06-02T04:17:20.312445519+00:00","last_message":"2021-06-02T05:33:21.692667024+00:00"}},"satellites_visible":0,"time_usec":0,"type":"GPS_RAW_INT","v_acc":0,"vel":0,"vel_acc":0},"HEARTBEAT":{"autopilot":{"type":"MAV_AUTOPILOT_ARDUPILOTMEGA"},"base_mode":{"bits":81},"custom_mode":19,"mavlink_version":3,"mavtype":{"type":"MAV_TYPE_SUBMARINE"},"message_information":{"counter":4563,"frequency":1.0,"time":{"first_message":"2021-06-02T04:17:17.848160794+00:00","last_message":"2021-06-02T05:33:21.539669615+00:00"}},"system_status":{"type":"MAV_STATE_ACTIVE"},"type":"HEARTBEAT"},"MISSION_COUNT":{"count":0,"message_information":{"counter":1,"frequency":null,"time":{"first_message":"2021-06-02T05:03:37.486328037+00:00","last_message":"2021-06-02T05:03:37.486349756+00:00"}},"mission_type":{"type":"MAV_MISSION_TYPE_MISSION"},"target_component":190,"target_system":255,"type":"MISSION_COUNT"},"MISSION_CURRENT":{"message_information":{"counter":45414,"frequency":9.95484447479248,"time":{"first_message":"2021-06-02T04:17:19.702201949+00:00","last_message":"2021-06-02T05:33:21.889268605+00:00"}},"seq":0,"type":"MISSION_CURRENT"},"NAMED_VALUE_FLOAT":{"message_information":{"counter":399816,"frequency":87.60210418701172,"time":{"first_message":"2021-06-02T04:17:17.857721885+00:00","last_message":"2021-06-02T05:33:21.912362180+00:00"}},"name":["S","t","i","c","k","M","o","d","e","\u0000"],"time_boot_ms":4587170,"type":"NAMED_VALUE_FLOAT","value":0.0},"NAV_CONTROLLER_OUTPUT":{"alt_error":8.696813583374023,"aspd_error":0.0,"message_information":{"counter":45414,"frequency":9.95484447479248,"time":{"first_message":"2021-06-02T04:17:19.702460695+00:00","last_message":"2021-06-02T05:33:21.892910002+00:00"}},"nav_bearing":158,"nav_pitch":22.816354751586914,"nav_roll":-178.67893981933594,"target_bearing":90,"type":"NAV_CONTROLLER_OUTPUT","wp_dist":0,"xtrack_error":0.0},"PARAM_VALUE":{"message_information":{"counter":1379,"frequency":0.496221661567688,"time":{"first_message":"2021-06-02T04:17:17.382040509+00:00","last_message":"2021-06-02T05:03:36.411921703+00:00"}},"param_count":689,"param_id":["R","C","1","6","_","D","Z","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000"],"param_index":688,"param_type":{"type":"MAV_PARAM_TYPE_INT16"},"param_value":0.0,"type":"PARAM_VALUE"},"POWER_STATUS":{"Vcc":4798,"Vservo":4826,"flags":{"bits":6},"message_information":{"counter":45414,"frequency":9.95484447479248,"time":{"first_message":"2021-06-02T04:17:19.682529932+00:00","last_message":"2021-06-02T05:33:21.886051991+00:00"}},"type":"POWER_STATUS"},"RAW_IMU":{"id":0,"message_information":{"counter":45397,"frequency":9.951117515563965,"time":{"first_message":"2021-06-02T04:17:19.734752711+00:00","last_message":"2021-06-02T05:33:21.877993635+00:00"}},"temperature":0,"time_usec":292202468,"type":"RAW_IMU","xacc":317,"xgyro":175,"xmag":-491,"yacc":167,"ygyro":-9,"ymag":266,"zacc":957,"zgyro":456,"zmag":-115},"RC_CHANNELS":{"chan10_raw":1100,"chan11_raw":1100,"chan12_raw":0,"chan13_raw":0,"chan14_raw":0,"chan15_raw":0,"chan16_raw":0,"chan17_raw":0,"chan18_raw":0,"chan1_raw":1500,"chan2_raw":1500,"chan3_raw":1500,"chan4_raw":1500,"chan5_raw":1300,"chan6_raw":1500,"chan7_raw":1500,"chan8_raw":1500,"chan9_raw":1100,"chancount":0,"message_information":{"counter":45418,"frequency":9.955720901489258,"time":{"first_message":"2021-06-02T04:17:19.711293621+00:00","last_message":"2021-06-02T05:33:21.917371628+00:00"}},"rssi":0,"time_boot_ms":4587170,"type":"RC_CHANNELS"},"SCALED_IMU2":{"message_information":{"counter":45397,"frequency":9.951117515563965,"time":{"first_message":"2021-06-02T04:17:19.736843145+00:00","last_message":"2021-06-02T05:33:21.879795741+00:00"}},"temperature":0,"time_boot_ms":4587169,"type":"SCALED_IMU2","xacc":350,"xgyro":168,"xmag":0,"yacc":23,"ygyro":-16,"ymag":0,"zacc":947,"zgyro":417,"zmag":0},"SCALED_PRESSURE":{"message_information":{"counter":45397,"frequency":9.951117515563965,"time":{"first_message":"2021-06-02T04:17:19.738746395+00:00","last_message":"2021-06-02T05:33:21.881683105+00:00"}},"press_abs":985.9927978515625,"press_diff":0.9619531035423279,"temperature":3839,"time_boot_ms":4587169,"type":"SCALED_PRESSURE"},"SERVO_OUTPUT_RAW":{"message_information":{"counter":45418,"frequency":9.955720901489258,"time":{"first_message":"2021-06-02T04:17:19.709378809+00:00","last_message":"2021-06-02T05:33:21.914680318+00:00"}},"port":0,"servo10_raw":0,"servo11_raw":0,"servo12_raw":0,"servo13_raw":0,"servo14_raw":0,"servo15_raw":0,"servo16_raw":0,"servo1_raw":1500,"servo2_raw":1500,"servo3_raw":1500,"servo4_raw":1500,"servo5_raw":1500,"servo6_raw":1500,"servo7_raw":1100,"servo8_raw":1500,"servo9_raw":0,"time_usec":292203152,"type":"SERVO_OUTPUT_RAW"},"STATUSTEXT":{"message_information":{"counter":6,"frequency":0.0021606050431728363,"time":{"first_message":"2021-06-02T04:17:17.906866746+00:00","last_message":"2021-06-02T05:03:34.950209089+00:00"}},"severity":{"type":"MAV_SEVERITY_INFO"},"text":["P","X","4","v","2"," ","0","0","2","6","0","0","3","8"," ","3","4","4","E","5","0","1","4"," ","2","0","3","9","3","8","4","E","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000","\u0000"],"type":"STATUSTEXT"},"SYSTEM_TIME":{"message_information":{"counter":90857,"frequency":19.916046142578125,"time":{"first_message":"2021-06-02T04:17:19.702351114+00:00","last_message":"2021-06-02T05:33:21.934951232+00:00"}},"time_boot_ms":4587189,"time_unix_usec":1622669680316000,"type":"SYSTEM_TIME"},"SYS_STATUS":{"battery_remaining":-1,"current_battery":4,"drop_rate_comm":0,"errors_comm":0,"errors_count1":0,"errors_count2":0,"errors_count3":0,"errors_count4":0,"load":320,"message_information":{"counter":45413,"frequency":9.954625129699707,"time":{"first_message":"2021-06-02T04:17:19.740726362+00:00","last_message":"2021-06-02T05:33:21.884494257+00:00"}},"onboard_control_sensors_enabled":{"bits":2137095},"onboard_control_sensors_health":{"bits":2161671},"onboard_control_sensors_present":{"bits":2161671},"type":"SYS_STATUS","voltage_battery":28},"VFR_HUD":{"airspeed":0.0,"alt":-8.6899995803833,"climb":0.0,"groundspeed":0.0,"heading":158,"message_information":{"counter":45610,"frequency":9.997807502746582,"time":{"first_message":"2021-06-02T04:17:19.718803757+00:00","last_message":"2021-06-02T05:33:21.919499092+00:00"}},"throttle":0,"type":"VFR_HUD"},"VIBRATION":{"clipping_0":0,"clipping_1":1,"clipping_2":0,"message_information":{"counter":45443,"frequency":9.961201667785645,"time":{"first_message":"2021-06-02T04:17:19.732746078+00:00","last_message":"2021-06-02T05:33:21.941986844+00:00"}},"time_usec":4587189930,"type":"VIBRATION","vibration_x":0.743248462677002,"vibration_y":1.1571483612060547,"vibration_z":1.0443675518035889}}
This is extracted from the NAMED_VALUE_FLOAT part of the JSON string.
"message_information": {
"counter": 302846,
"frequency": 87.47718048095703,
"time": {
"first_message": "2021-06-02T04:17:17.857721885+00:00",
"last_message": "2021-06-02T05:15:00.035238554+00:00"
}
},
"name": [
"P",
"i",
"l",
"o",
"t",
"G",
"a",
"i",
"n",
"\u0000"
],
"time_boot_ms": 3485250,
"type": "NAMED_VALUE_FLOAT",
"value": 0.5
}
This is the working code provided by Remi Lebeau to get the value out.
function TForm_OV_REC.ParseJSON(sJSON, sITEM, sVALUE : String) : String;
var
LJsonVal : TJSONValue;
LJPair : TJSONPair;
begin
Result := '';
LJsonVal := TJSONObject.ParseJSONValue(sJSON);
if LJsonVal = nil then Exit;
try
LJPair := (LJsonVal as TJSONObject).Get(sITEM);
if LJPair = nil then Exit;
LJPair := (LJPair.JsonValue as TJSONObject).Get(sVALUE);
if LJPair = nil then Exit;
Result := LJPair.JsonValue.Value;
finally
LJsonVal.Free;
end;
end;
procedure TForm_OV_REC.timerRESTAPITimer(Sender: TObject);
var
MyString, sJSON : String;
IdHTTP: TIdHTTP;
begin
IdHTTP := TIdHTTP.Create(nil);
try
IdHTTP.ConnectTimeout := 100;
sJSON := IdHTTP.Get('http://192.168.2.2:4777/mavlink');
finally
IdHTTP.Free;
end;
MyString := ParseJSON(sJSON, 'NAMED_VALUE_FLOAT', 'value');
...
end;
The "name" array changes from PilotGain to Tethertrn etc etc and I need to retrieve it to associate the right value. My knowledge of JSON is very limited. I honestly have no clue how to proceed.
Cheers, E.
================================================= Clarifications:
The robot sends a JSON to URL http://192.168.2.2:4777/mavlink about 20 time per second. For some reason I don't understand they use item NAMED_VALUE_FLOAT to pass values for either PilotGain,Stickmode, TetherTrn,etc,etc And for further reason I don't understand, instead of just using a string for "name", they use an array...
NAMED_VALUE_FLOAT is the item like ATTITUDE in the previous question.
I can get the "value" from NAMED_VALUE_FLOAT but this value means nothing unless I can tie it in with its identifier. Is the value for PilotGain? for TetherTrn? etc.
The part I am trying to retrieve is:
"name": [
"P",
"i",
"l",
"o",
"t",
"G",
"a",
"i",
"n",
"\u0000"
],
Where the "name" array contains "P","i","l","o","t","G","a","i","n","\u0000"
So I need to retrieve: PilotGain
This name array changes to other characters such as:
"S","t","i","c","k","M","o","d", "e","\u0000"
So I need to retrieve: StickMode
"T","e","t","h","e","r","T","r","n","\u0000"
So I need to retrieve: TetherTrn
So on...
If I try to retrieve the value of "name" I get an empty string. So I am guessing I have to retrieve the array in Name and then put that into a string.
I am sorry, I cannot change what the robot puts out as JSON.
I hope this is more clear.
Thank you.