Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am getting a JSON response back from a server and the array I need to grab is a value embedded deep inside multiple objects and arrays. I need to grab the formatIds JSONArray. My JSON looks like this:

{
    "entries": [
        {
            "title": "sample",
            "targets": [
                {
                    "format": "wav",`enter code here`
                    "description": "A wav file",
                    "formatCriteria": [
                        {
                            "formatSelectionTitle": "Find WAV files",
                            "formatSteps": {
                                "UniqueId": "214212312321",
                                "formatMatches": {
                                    "formatIds": [
                                        "WAV",
                                        "MP3"
                                    ]
                                }
                            }
                        }
                    ]
                }
            ]
        }
    ]
}

The only way I have been able to figure out is to have a bunch of embedded for loops:

String[] assetUri;
for (int i = 0; i < entriesArray.length(); i++) {
    entryJsonOjbect = entriesArray.getJSONObject(i);
    fileTargetArray = tempObj.getJSONArray("targets");
    //Loop through the targets array
    for (int j = 0; j < fileTargetArray.length(); j++) {
        tempObj = fileTargetArray.getJSONObject(j);
        fileCriteriaArray = tempObj.getJSONArray("formatCriteria");
        //Loop through fileCriteria Array
        for (int h = 0; h < fileCriteriaArray.length(); h++) {
            JSONObject fileCriteriaObj = fileCriteriaArray.getJSONObject(h);
            //Get the JSON Object 'formatSteps'
            JSONObject selectStepObj = fileCriteriaObj.getJSONObject("formatSteps");
            //Get the JSON Object 'formatMatches'
            JSONObject selectionMatches = selectStepObj.getJSONObject("formatMatches");
            //FINALLY. Get the formatIds ARray
            JSONArray assetTypeArray = selectionMatches.getJSONArray("formatIds");
                //Assign the values from the JSONArray to my class
                assetUri = new String[assetTypeArray.length()];
                for (int z = 0; z < assetTypeArray.length(); z++) {
                    assetUri[z] = assetTypeArray.getString(z);
            }

        }
    }
}

This code REALLY REALLY slow x20 because of all the embedded loops. Is there a way for me to get this JSONArray without having to have this extra code?? In jQuery there is a JSON.find and was wondering if there something similar to this in Java? I have tried the JSON API calls getJSONArray and get on the root jsonObject but it doesn't work unless you are in object in which the json array exists. I'm sure I could do some type of recursive solution but this is still annoying. Any advice??

share|improve this question
up vote 1 down vote accepted

Parsing JSON with the org.json-library is like parsing XML with DOM: Step by step. But: You'll need all these loops.

If your JSON response always looks like the one you posted (a single element in every array), your JSON is very ineffective. If you just want the "first" entries, don't loop over the arrays.

Parsing such a complex data-structure with org.json can be a pain. Have you tried the (more automated) google-gson library? Check out the examples. Although I don't want to say that parsing it with gson will be faster in terms of CPU time, it will deficiently be faster in terms of development time.

I also just recently wrote a blog-post about this topic: JSON and Java

share|improve this answer

You could use a library such as GSON to parse into Java objects for you instead of transcending and looping by hand.

http://java.sg/parsing-a-json-string-into-an-object-with-gson-easily/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.