Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - Parse JSON data using JSONReader or JSONObject/JSONArray

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
                        }
                    ]
                }
            ]
        }
    ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...