Skip to content Skip to sidebar Skip to footer

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://affilia

Solution 1:

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

Solution 2:

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/valuesString 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.

Solution 3:

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.

Solution 4:

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

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

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

                    //  get id from  issueCatagory[i] = issue.optString("apiName";
                    i++;
                }
                JSONObject[] objcat = newJSONObject[Catagory.length];
                CatagoryName=newString[Catagory.length];
                Url=newString[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";

                }

Post a Comment for "How To Deal With Nested Json Objects In Android"