The following JSON Object (a variable named json_object
) is composed of two values
- peripheral_devices
- peripheral_tests
both of which are arrays:
{
"peripheral_devices": [
{
"_id": 1,
"active": -1,
"battery": "63",
"bt_firmware_version": "btv1.7.777",
"configured": 0,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323224,
"device_id": 72,
"discovered": 0,
"firmware_version": "v3.14",
"hardware_version": "null",
"icon": "icon_device_bp5",
"last_connect_date": 1640023710420,
"mac_address": "8C:DE:52:41:FC:57",
"model": "BP5 41FC57",
"name": "BP5",
"other_id": "-1",
"paired": -1,
"type_id": 1,
"update_date": 1635807323224,
"user_id": 13
},
{
"_id": 3,
"active": -1,
"battery": "90",
"bt_firmware_version": "1.0.0",
"configured": -1,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323239,
"device_id": 72,
"discovered": 0,
"firmware_version": "1.0.0",
"hardware_version": "5.0.0",
"icon": "icon_device_hs2s",
"last_connect_date": 1640022147928,
"mac_address": "00:4D:32:0C:B7:2C",
"model": "HS2S 11070",
"name": "HS2S",
"other_id": "-1",
"paired": -1,
"type_id": 3,
"update_date": 1635807323239,
"user_id": 13
}
],
"peripheral_tests": [
{
"_id": 199,
"consumer_id": 22,
"create_date": 1640020949760,
"end_date": 1640020949078,
"mood_id": -1,
"notes": "",
"start_date": 1640020939533,
"status_id": 1,
"type_id": 2,
"update_date": 0,
"user_id": -99
},
{
"_id": 198,
"consumer_id": 22,
"create_date": 1640020904183,
"end_date": -1,
"metric_bps": {,
"mood_id": -1,
"notes": "",
"start_date": 1640020863742,
"status_id": 1,
"type_id": 1,
"update_date": 0,
"user_id": -99
},
{
"_id": 197,
"consumer_id": 22,
"create_date": 1640020834664,
"end_date": 1640020828741,
"mood_id": -1,
"notes": "",
"start_date": 1640020822580,
"status_id": 1,
"type_id": 3,
"update_date": 0,
"user_id": -99
}
]
}
and derived as follows:
JSONObject json_object = new JSONObject(post_data);
Using org.json.JSONArray.JSONArray
:
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_devices");
works as expected and a json array is parsed from json_object
. However, using the same technique to get the second array in the object does not:
JSONArray json_tests = new JSONArray("peripheral_tests");
throws exception:
A JSONArray text must start with '[' at 1 [character 2 line 1]
Why? What am I not understanding?
CodePudding user response:
You code
JSONArray json_tests = new JSONArray("peripheral_tests");
Take string in the constructor which is the JSON data and peripheral_tests
is not JSON array, to get peripheral_tests you need to get it from json_object
like
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_tests");
CodePudding user response:
The input JSON is invalid, accoring to JSON Lint ...which may lead to unexpected results:
Error: Parse error on line 69:
..."metric_bps": { , "mood_id": -1,
----------------------^
Expecting 'STRING', '}', got ','