如何在android中处理嵌套的json OBJECTS

时间:2015-01-10 09:35:34

标签: android json

如何在android下面阅读json?请使用JSONObject jsonObject;

提供解决方案

我想得到这个

{
    "resourceName": "bags_wallets_belts",
    "get": "https://affiliate-api.flipkart.net/affiliate/feeds/youraffiliateid/category/v1:reh.json?expiresAt=1420910131274&sig=7db02590dfaea9a22f88c869d8035d05",
    "post": null,
    "put": null,
    "delete": null
}

使用以下json:

{
"title": "Flipkart Affiliate API Directory",
"description": "This directory contains information about all the affiliate API's and their versions",
"apiGroups": {
    "affiliate": {
        "name": "affiliate",
        "apiListings": {
            "bags_wallets_belts": {
                "availableVariants": {
                    "v0.1.0": {
                        "resourceName": "bags_wallets_belts",
                        "get": "https://affiliate-api.flipkart.net/affiliate/feeds/youraffiliateid/category/v1:reh.json?expiresAt=1420910131274&sig=7db02590dfaea9a22f88c869d8035d05",
                        "post": null,
                        "put": null,
                        "delete": null
                    }
                },
                "apiName": "bags_wallets_belts"
            },
            "washing_machine": {
                "availableVariants": {
                    "v0.1.0": {
                        "resourceName": "washing_machine",
                        "get": "https://affiliate-api.flipkart.net/affiliate/feeds/youraffiliateid/category/v1:j9e-abm-8qx.json?expiresAt=1420910131275&sig=7c48abab9d35ae77fff3d998e1a2efdc",
                        "post": null,
                        "put": null,
                        "delete": null
                    }
                },
                "apiName": "washing_machine"
            },
            "mens_footwear": {
                "availableVariants": {
                    "v0.1.0": {
                        "resourceName": "mens_footwear",
                        "get": "https://affiliate-api.flipkart.net/affiliate/feeds/youraffiliateid/category/v1:osp-cil.json?expiresAt=1420910131274&sig=f7ac9d1c19ae39b226c0ca46725d609d",
                        "post": null,
                        "put": null,
                        "delete": null
                    }
                },
                "apiName": "mens_footwear"
            }
        }
    }
}

}

提前致谢。

4 个答案:

答案 0 :(得分:2)

您也可以试试这个。

下面将为您解析一个节点,您可以完成剩下的工作。

JSONObject jMainObject = null;
    try {
        jMainObject = new JSONObject(jsonString);
        JSONObject apiGroupsObject = jMainObject.getJSONObject("apiGroups");
        JSONObject affiliateObject = apiGroupsObject.getJSONObject("affiliate"); 
        JSONObject apiListingsObject = affiliateObject.getJSONObject("apiListings");
        JSONObject bags_wallets_beltsObject = apiListingsObject.getJSONObject("bags_wallets_belts");
        JSONObject availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
        JSONObject versionObject = availableVariantsObject.getJSONObject("v0.1.0");
        System.out.println(versionObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }

答案 1 :(得分:1)

假设您的JSONObject已经定义为“rootObject”:

JSONObject apiGroupsObject = rootObject.getJSONObject("apiGroups");
JSONObject affiliateObject = apiGroupsObject.getJSONObject("affiliate");
JSONObject apiListingsObject = affiliateObject.getJSONObject("apiListings");
JSONObject bagsWalletsBeltsObject = apiListingsObject.getJSONObject("bags_wallets_belts");
JSONObject availableVariantsObject = bagsWalletsBeltsObject.getJSONObject("availableVariants");
JSONObject versionObject = availableVariantsObject.getJSONObject("v0.1.0");

// now you can start picking out the keys/values
String resourceName = versionObject.getString("resourceName");
// etc.

这不是挖掘你所追求的物体的最佳方式,但希望它能让你知道它是如何工作的。查找GSON库以获得更高效的JSON解析。

答案 2 :(得分:0)

我的回答可能会被低估,因为您需要使用JSONObject的解决方案。

你可以用库做同样的事情我开发了https://github.com/bajicdusko/AndroidJsonProvider

通过使用Java泛型和使用递归,可以序列化无限数量的嵌套JSONObject。你得到的结果是你的模型类,但除此之外,完整的JSONObject也可以在Provider类中找到,所以部分我已经回答了你的问题。

如果我能提供帮助,我很高兴。

答案 3 :(得分:0)

这样做,希望它会帮助你,测试

JSONObject objresponse = new JSONObject(response);
                    JSONObject objapiGroups = objresponse.getJSONObject("apiGroups";
                    JSONObject objaffiliate = objapiGroups.getJSONObject("affiliate";
                    JSONObject objapiListings = objaffiliate.getJSONObject("apiListings";

                Iterator iterator = objapiListings.keys();
                int i=0;
                Catagory=new String[objapiListings.length()];
                while(iterator.hasNext()){
                    String key = (String)iterator.next();
                    JSONObject issue = objapiListings.getJSONObject(key);

                    //  get id from  issue
                    Catagory[i] = issue.optString("apiName";
                    i++;
                }
                JSONObject[] objcat = new JSONObject[Catagory.length];
                CatagoryName=new String[Catagory.length];
                Url=new String[Catagory.length];
                for(int j=0;j<Catagory.length;j++)
                {
                    objcat[j]=objapiListings.getJSONObject(Catagory[j]);

                    CatagoryName[j]=objcat[j].getJSONObject("availableVariants".getJSONObject("v1.1.0".getString("resourceName";
                    Url[j]=objcat[j].getJSONObject("availableVariants".getJSONObject("v1.1.0".getString("get";

                }