0

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.

mjp66
  • 4,214
  • 6
  • 26
  • 31
Rajat Ghai
  • 31
  • 3
  • does [this](http://ankursinghal86.blogspot.in/2014/11/json-to-hashmap-parsing-json-string.html) helps you – Ankur Singhal Jan 10 '15 at 09:41
  • can u pls. provide solution for above json? – Rajat Ghai Jan 10 '15 at 09:43
  • basically you have list `apiListings`, and you need to access its elements, so basically the above example covers that part as well. Populate the map as explained, and just using a key access the map and get list – Ankur Singhal Jan 10 '15 at 09:51

4 Answers4

2

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();
    }
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
1

Assume your JSONObject above is already defined as, say, "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.

This isn't really the best way of digging down to the object you're after but hopefully it'll give you an idea of how it works. Look up the GSON library for more efficient JSON parsing.

mjp66
  • 4,214
  • 6
  • 26
  • 31
  • how to get keys?? JSONObject bagsWalletsBeltsObject = apiListingsObject.getJSONObject("bags_wallets_belts"); – Rajat Ghai Jan 10 '15 at 10:20
  • It's already shown for you in my answer: String resourceName = versionObject.getString("resourceName"); ... this gives you the value of "resourceName" which in your example is "washing_machine". Follow the same procedure for the rest of the key/value items in the object. – mjp66 Jan 10 '15 at 11:26
0

My answer could be downvoted, because you want solution with JSONObject usage.

The same thing you can do with library i have developed https://github.com/bajicdusko/AndroidJsonProvider.

With usage of Java generics and with usage of recursion, infinite number of nested JSONObject's can be serialized. What you get as a result is your model class, but besides that, complete JSONObject also can be found in Provider class, so partially i have answered your question.

Glad if i could help.

bajicdusko
  • 1,630
  • 1
  • 17
  • 32
0

do it like this, hope it will help you, tested

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

                }
Atul Mavani
  • 1,437
  • 1
  • 10
  • 11