Rajat Ghai Rajat Ghai - 5 months ago 19
JSON Question

How to deal with nested json OBJECTS in android

How to read below json in android? Please provide solution using

JSONObject jsonObject;


I want to get this

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


by using below 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"
}
}
}
}


}

Thanks in advance.

Answer

You can try this as well .

The below will parse a single node for you and you can do the rest.

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