1

How do I parse Json data when the Json response data is returned in nested Json object form.

I looked at this post which shows how to get only one piece of data. As I want whole data set it is not useful for me.

It said:

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

The Json returned data which I want to parse is coming from here.

Can anyone help me to solve my problem about how to parse this type of data as I’m new so I don’t know how to parse nested Json object data. Perhaps I can use Gson for it? But I also don't know how to use that.

4
  • Possible duplicate of android-parsing nested json Objects Commented Jun 28, 2016 at 11:10
  • Use gson. It is very fast and easy way to perse data from json. Remember that the object name must be the same as the key. Gson lib use reflection to parse data. Commented Jun 28, 2016 at 11:17
  • how to use Gson lib? Commented Jun 28, 2016 at 11:20
  • check these.... for all System.out.println(apiGroupsObject ); System.out.println(affiliateObject); etc... see what the prob... Commented Jun 28, 2016 at 11:21

4 Answers 4

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

                    }
Sign up to request clarification or add additional context in comments.

Comments

0

The first rule of serializing/deserialize - don't do it yourself. Use compile 'com.google.code.gson:gson:2.6.2' instead. It is the google annotation based deserializer. Here is some tutorial on how to use it.

Comments

0
Try your parsing using volley library .It's a big data so always use optJson object for avoiding null.This is small piece of code .Hope it will help you.
  String title = response.getString("title");
String   description = response.getString("description");


JSONObject objApiGrp = response.optJSONObject("apiGroups");
if(objApiGrp != null && objApiGrp.size()>0)
{
    JSONObject objaffiliate = objApiGrp.optJSONObject("affiliate");

    for(int i=0 ; i<objaffiliate.size() ; i++)
{


JSONObject objapiListings = objaffiliate.optJSONObject("apiListings");
for(int i=0 ; i<objapiListings.size() ; i++)
{

//      parse all objects


}


}

Comments

0

You need to make all separate objects to get whole key data value. Like this:

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

    JSONObject fragrances = apiListingsObject.getJSONObject("fragrances");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject tv_video_accessories = apiListingsObject.getJSONObject("tv_video_accessories");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject camera_accessories = apiListingsObject.getJSONObject("camera_accessories");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    JSONObject sports_fitness = apiListingsObject.getJSONObject("sports_fitness");
    availableVariantsObject = bags_wallets_beltsObject.getJSONObject("availableVariants");
    versionObject = availableVariantsObject.getJSONObject("v0.1.0");
    System.out.println(versionObject);

    // Make and use object like this for all key Values.

} catch (JSONException e) {
    e.printStackTrace();
}

All Keys are different so, not able to use loop for that. otherwise code is optimize with loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.