如何解析这个嵌套的JSON数组中的机器人嵌套、机器人、组中、JSON

2023-09-12 06:35:40 作者:繁花凋零、秋

我要解析下面的嵌套JSON数组的数据到我的应用程序。我很困惑如何获取值出来。

  {
            prodCat_list:[
                {
                    刺针:
                        {
                            cat_id:9,
                            位置:1,
                            SKU:wwww345

                        },
                        {
                            cat_id:9,
                            位置:2,
                            SKU:coof23

                        },
                        {
                            cat_id:9,
                            位置:3,
                            SKU:dde45

                        },
                        {
                            cat_id:9,
                            位置:4,
                            SKU:5555

                        }
             ]
                },
{
                    刺针:
                        {
                            cat_id:9,
                            位置:1,
                            SKU:wwww345

                        },
                        {
                            cat_id:9,
                            位置:2,
                            SKU:coof23

                        },
                        {
                            cat_id:9,
                            位置:3,
                            SKU:dde45

                        },
                        {
                            cat_id:9,
                            位置:4,
                            SKU:5555

                        }
             ]
                },
            ]
        }
 

任何人都可以请指导我如何从拿到里面的值。

我已经试过这种

  JSONParser分析器=新JSONParser();
        JSONObject的项目= parser.getJSONFromUrl(productInfoUrl);
        尝试 {
            JSONArray itemsDetails = items.getJSONArray(prodCat_list);
            如果(itemsDetails.length()大于0){

                的for(int i = 0; I< itemsDetails.length();我++){
                    JSONArray productWithCategories = itemsDetails.getJSONArray(ⅰ);
                    的JSONObject对象= productWithCategories.getJSONObject(我);

                    产品productInfo =新产品(object.getString(SKU),object.getInt(cat_id),object.getInt(位置));
                    ProductDbHandler productDbHandler =新ProductDbHandler(上下文);
                    productDbHandler.addProducts(productInfo);
                }
            }
            其他
                的System.out.println(没有要添加的产品);
        }赶上(JSONException E){
            e.printStackTrace();
        }
    }
 

解决方案 从字符串中解析出JSON

下面是我想你的JSON解析器应该像(可以有一些错字错,我没有测试在编辑这个code:) ):

 的JSONObject mainObj =新的JSONObject(MyString的);
如果(mainObj!= NULL){
    JSONArray列表= mainObj.getJSONArray(prodCat_list);
    如果(名单!= NULL){
        的for(int i = 0; I< list.length();我++){
            JSONObject的ELEM = list.getJSONObject(我);
            如果(ELEM!= NULL){
                JSONArray电棒= elem.getJSONArray(刺针);
                如果(刺针!= NULL){
                    对于(INT J = 0; J< prods.length(); J ++){
                        JSONObject的innerElem = prods.getJSONObject(J);
                        如果(innerElem!= NULL){
                            INT cat_id = innerELem.getInt(cat_id);
                            INT POS = innerElem.getInt(位置);
                            字符串SKU = innerElem.getString(SKU);
                        }
                    }
                }
            }
        }
    }
}
 

I have to parse the below nested Json array's data into my application. I am confused how to get the values out of it.

  {
            "prodCat_list": [
                {
                    "prods": [
                        {
                            "cat_id": "9",
                            "position": "1",
                            "sku": "wwww345"

                        },
                        {
                            "cat_id": "9",
                            "position": "2",
                            "sku": "coof23"

                        },
                        {
                            "cat_id": "9",
                            "position": "3",
                            "sku": "dde45"

                        },
                        {
                            "cat_id": "9",
                            "position": "4",
                            "sku": "5555"

                        }
             ]
                },
{
                    "prods": [
                        {
                            "cat_id": "9",
                            "position": "1",
                            "sku": "wwww345"

                        },
                        {
                            "cat_id": "9",
                            "position": "2",
                            "sku": "coof23"

                        },
                        {
                            "cat_id": "9",
                            "position": "3",
                            "sku": "dde45"

                        },
                        {
                            "cat_id": "9",
                            "position": "4",
                            "sku": "5555"

                        }
             ]
                },
            ]
        }

Can anyone please guide me how to get the inside values from that.

I have tried this

JSONParser parser = new JSONParser();
        JSONObject items = parser.getJSONFromUrl(productInfoUrl);
        try {
            JSONArray itemsDetails = items.getJSONArray("prodCat_list");
            if(itemsDetails.length()>0){

                for (int i = 0; i < itemsDetails.length(); i++) {
                    JSONArray productWithCategories = itemsDetails.getJSONArray(i);
                    JSONObject object = productWithCategories.getJSONObject(i);

                    Product productInfo = new Product( object.getString("sku"), object.getInt("cat_id"), object.getInt("position"));
                    ProductDbHandler productDbHandler = new ProductDbHandler(context);
                    productDbHandler.addProducts(productInfo);
                }
            }
            else 
                System.out.println("No product to add");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

解决方案

Here is how I think your JSON Parser should look like (there can be some typo mistakes,I didn't test this code on editor : )) :

JSONObject mainObj = new JSONOBject(myString);
if(mainObj != null){
    JSONArray list = mainObj.getJSONArray("prodCat_list");
    if(list != null){
        for(int i = 0; i < list.length();i++){
            JSONObject elem = list.getJSONObject(i);
            if(elem != null){
                JSONArray prods = elem.getJSONArray("prods");
                if(prods != null){
                    for(int j = 0; j < prods.length();j++){
                        JSONObject innerElem = prods.getJSONObject(j);
                        if(innerElem != null){
                            int cat_id = innerELem.getInt("cat_id");
                            int pos = innerElem.getInt("position");
                            String sku = innerElem.getString("sku");
                        }
                    }
                }
            }
        }
    }
}