使用JSONReader解析JSON数据或的JSONObject / JSONArray数据、JSON、JSONReader、JSONObject

2023-09-06 23:22:50 作者:、、卟停留好不好、、卟在意好不好

我有一些JSON(如下图所示),我试图分析整个JSON,每个对象将是声明以下变量的类的新实例。什么是做到这一点的最好方法是什么?我应该使用一个JSONReader或使用的JSONObject和JSONArray。香港专业教育学院一直在阅读一些教程,并提出一些一般性的问题,但我还没有看到如何解析出数据的任何这样的例子。

  {
    ID:356,
    hassubcategories:真实,
    子:
        {
            ID:3808,
            类别名称:当前产品,
            CategoryImage:空,
            hassubcategories:真实,
            子:
                {
                    ID:4106,
                    类别名称:建筑,
                    CategoryImage:2637,
                    hassubcategories:真实,
                    子:
                        {
                            ID:391,
                            类别名称:地板,
                            CategoryImage:2745,
                            hassubcategories:假的
                        }
                    ]
                }
            ]
        },
        {
            ID:3809,
            类别名称:非当前产品,
            CategoryImage:空,
            hassubcategories:真实,
            子:
                {
                    ID:4107,
                    类别名称:台,
                    CategoryImage:2638,
                    hassubcategories:真实,
                    子:
                        {
                            ID:392,
                            类别名称:木,
                            CategoryImage:2746,
                            hassubcategories:假的
                        }
                    ]
                }
            ]
        }
    ]
}
 

解决方案

如果我要做到这一点,我会分析整个字符串到JSONObject的

 的JSONObject的obj =新的JSONObject(STR);
 
JSON JSONObject 键值对数据的读取

然后我看到你的子类别是一个JSONArray。所以我会像这样转换

  JSONArray ARR =新JSONArray(obj.get(子));
 

这个你可以做一个循环,并实例化类对象

 的for(int i = 0; I< arr.length;我++)
JSONObject的温度= arr.getJSONObject(我);
C类=新类别();
c.setId(temp.get(ID));
 

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
                        }
                    ]
                }
            ]
        }
    ]
}

解决方案

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"));