Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have some JSON (shown below), I am trying to parse through the entire JSON, and each object will be a new instance of a class that declares the variables below. What is the best way to do this? Should I use a JSONReader or use JSONObject and JSONArray. Ive been reading some tutorials and asking some general questions, but I havent seen any examples of how to parse out data like this.

{
    "id": 356,
    "hassubcategories": true,
    "subcategories": [
        {
            "id": 3808,
            "CategoryName": "Current Products",
            "CategoryImage": null,
            "hassubcategories": true,
            "subcategories": [
                {
                    "id": 4106,
                    "CategoryName": "Architectural",
                    "CategoryImage": "2637",
                    "hassubcategories": true,
                    "subcategories": [
                        {
                            "id": 391,
                            "CategoryName": "Flooring",
                            "CategoryImage": "2745",
                            "hassubcategories": false
                        }
                    ]
                }
            ]
        },
        {
            "id": 3809,
            "CategoryName": "Non-Current Products",
            "CategoryImage": null,
            "hassubcategories": true,
            "subcategories": [
                {
                    "id": 4107,
                    "CategoryName": "Desk",
                    "CategoryImage": "2638",
                    "hassubcategories": true,
                    "subcategories": [
                        {
                            "id": 392,
                            "CategoryName": "Wood",
                            "CategoryImage": "2746",
                            "hassubcategories": false
                        }
                    ]
                }
            ]
        }
    ]
}
share|improve this question

4 Answers 4

up vote 1 down vote accepted

if i were to do it, i will parse the whole string to a JSONObject

JSONObject obj = new JSONObject(str);

then i see that your subcategories is an JSONArray. So i will convert it like this

JSONArray arr = new JSONArray(obj.get("subcategories"));

with this you can do a loop and instantiate your class object

for(int i = 0; i < arr.length; i++)
JSONObject temp = arr.getJSONObject(i);
Category c = new Category();
c.setId(temp.get("id"));
share|improve this answer

GSON is the easiest way when you have to work with nested objects.

like this:

//after the fetched Json:
Gson gson = new Gson();

Event[] events = gson.fromJson(yourJson,  Event[].class);

//somewhere nested in the class:
static class Event{
    int id;
    String categoryName;
    String categoryImage;
    boolean hassubcategories;
    ArrayList<Event> subcategories;
}

You can check this tutorial, http://androidsmith.com/2011/07/using-gson-to-parse-json-on-android/ or http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html or http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

share|improve this answer

The example JSON data you posted does not seem to follow the JSON's data structure. You will need to construct you data exactly the same way as taught in the third link posted by Mustafa. This is a really great tutorial. I followed the steps and it really works!

share|improve this answer

You can use JSON Object/ JSON Array only if the size of your json data is less than 1MB. Else you should go with JSONReader. JSONReader actually use streaming approach while JSONObject and JSONArray eventually load all the data on RAM at once which causes OutOfMemoryException in case of bigger json.

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.