4

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

3
  • There is an issue when trying to target the content when converting to string and logging it shows illformed json. The output is shown above, random keys are appearing like value= which I believe is messing with the system? I can't even target the values because it's a bad json string. Commented May 28, 2015 at 1:34
  • Have you looked into using regular expression? Commented Jun 2, 2015 at 7:08
  • Regular expression wouldn't of been much help, thankfully I was able to solve it with the two answer below. GSON was modifying the data when it creates a json string, so I ditched it all together. Commented Jun 4, 2015 at 21:46

2 Answers 2

3

Instead of pulling the string value of content out of your JSONObject, you can get a JSONArray instead

JSONArray content = mediaObject.getJSONArray("content");

Now you can loop through the objects in your array with a pretty conventional for loop

for(int i = 0; i < content.length(); i++) {
    JSONObject mediaItem = content.getJSONObject(i);
    boolean itemIsDefault = mediaItem.getBoolean("isDefault");
}

Here are the links to all the JSONObject methods and JSONArray methods

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this earlier and got the error Unhandled exception: org.json.JSONException. Is there a way that doesn't throw null if it doesn't exist? In this case content always exists
You need to check if(jsonObj.has("key_like_isDefault")) and then try to pull your required values
Do you mean doesn't throw null or doesn't throw an exception? If it's the latter, you can use mediaObject.optJSONArray("content")
Interesting, the loop throws Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
While this is partially right, the reason it wasn't working was due to gson converting. It was changing the json string as shown in the question. So when I went to convert it back into an JSONObject it wasn't playing ball.
2

Can you not just

JSONObject mJson = new JSONObject(inputString);

why are you using Gson?

1 Comment

No issue with how the json is produced, it's just an error targeting the data inside. I can't target arrays, it returns null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.