如何在android中解析这个嵌套的JSON数组 [英] How to parse this nested JSON array in android

查看:55
本文介绍了如何在android中解析这个嵌套的JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将下面嵌套的 Json 数组的数据解析到我的应用程序中.我很困惑如何从中获得价值.

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.

我已经试过了

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();
        }
    }

推荐答案

这是我认为您的 JSON 解析器应该是什么样子的(可能有一些拼写错误,我没有在编辑器上测试这段代码 :)) :

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

这篇关于如何在android中解析这个嵌套的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆