Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON data using JSONReader or JSONObject/JSONArray

Tags:

java

json

android

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
                        }
                    ]
                }
            ]
        }
    ]
}
like image 373
ios85 Avatar asked Feb 07 '13 03:02

ios85


People also ask

What is the difference between JSON object and JSONArray?

JSONObject and JSONArray are the two common classes usually available in most of the JSON processing libraries. A JSONObject stores unordered key-value pairs, much like a Java Map implementation. A JSONArray, on the other hand, is an ordered sequence of values much like a List or a Vector in Java.

How do I check if a String is Jsonarject or JSONArray?

JSONObject json = RestManager. getJSONfromURL(myuri); // retrieve the entire json stream JSONArray interventionJsonArray = json. getJSONArray("intervention");

What is JSON object parse?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. In Javascript, the standard way to do this is by using the method JSON. parse() , as the Javascript standard specifies.


1 Answers

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.

like image 51
Yasir Ali Avatar answered Sep 20 '22 00:09

Yasir Ali