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.

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

4 Answers4

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.

loshkin
  • 1,600
  • 2
  • 21
  • 38
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


}


}
Gevaria Purva
  • 552
  • 5
  • 15
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.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
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");

                    }
gaurang
  • 2,217
  • 2
  • 22
  • 44