I'm querying out and getting a json string returned, for the sake of this example I'll post an example. I'm trying to figure out how I would go about digging through the array of values and find which is marked as default.
Example JSON
{
"id": "333706819617",
"guid": "4aCdriCG0WvfYEUkFf8_xqQEFxgwgNU8",
"title": "Test entry",
"author": "",
"description": "Desc",
"added": 1411702963000,
"content": [
{
"audioChannels": 2,
"audioSampleRate": 44100,
"bitrate": 281656,
"checksums": {
"md5": "70DF3E21131F9F02C3F0A74F3664AB73"
},
"contentType": "audio",
"duration": 43.258,
"expression": "full",
"fileSize": 1522986,
"frameRate": 0.0,
"format": "AAC",
"height": 288,
"isDefault": false,
"language": "en",
"sourceTime": 0.0,
"url": "http://example.com/dZiASoxchRyS",
"width": 352
},
{
"audioChannels": 2,
"audioSampleRate": 44100,
"bitrate": 160000,
"checksums": {
"md5": "3AC622D31B9DED37792CC7FF2F086BE6"
},
"contentType": "audio",
"duration": 43.206,
"expression": "full",
"fileSize": 866504,
"frameRate": 0.0,
"format": "MP3",
"height": 0,
"isDefault": false,
"language": "",
"sourceTime": 0.0,
"url": "http://example.com/59M_PSFgGGXE",
"width": 0
}
],
"thumbnails": [
{
"audioChannels": 0,
"audioSampleRate": 0,
"bitrate": 0,
"checksums": {
"md5": "BE8C98A07B3FE9020BFA464C42112999"
},
"contentType": "image",
"duration": 0.0,
"expression": "full",
"fileSize": 20379,
"frameRate": 0.0,
"format": "JPEG",
"height": 256,
"isDefault": true,
"language": "",
"sourceTime": 0.0,
"url": "http://img.example.com/waveform.jpg",
"width": 256
}
]
}
I take the JSON string and convert it back into a JSONObject
JSONObject mediaObject = new Gson().fromJson(mediaString, JSONObject.class);
String content = mediaObject.optString("content");
When I output content it returns the following.
{values=[{nameValuePairs={audioChannels=2.0, audioSampleRate=44100.0, bitrate=281656.0, checksums={nameValuePairs={md5=70DF3E21131F9F02C3F0A74F3664AB73}}......
How do I correctly step through the values of content and find the value of isDefault? In the example JSON there is no content where isDefault = true so it would default to the first object.
Seems I can only target the values as string, do I have to cast content as a JSONArray?
Edit: I can't seem to convert mediaObject.content into a JSONArray. mediaObject.optJSONArray("content") returns null. I've also tried getting it as a string then converting into a JSONArray with no prevail.
Edit 2: Found what the issue with the data was, when I was parsing the json with gson, it was messing with the final outputted data.
So I changed from new Gson().toJson(jsonObject); to jsonObject.toString()) and I could now target the arrays using optJSONArray. To get them data back into a JSONObject, I used JSONObject mediaObject = new JSONObject(mediaString);
GSON was altering the data
contentwhen converting to string and logging it shows illformed json. The output is shown above, random keys are appearing likevalue=which I believe is messing with the system? I can't even target thevaluesbecause it's a bad json string.