如何处理 android 中的嵌套 json 对象

How to deal with nested json OBJECTS in android

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

}

提前致谢。

假设上面的 JSON对象已经定义为 "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 解析。

你也可以试试这个。

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

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

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

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

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

很高兴能帮上忙。

照着做,希望对你有帮助,已测试

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

                }