1

I've usually use Retrofit to do all parsing "dirty" work, but recently I've decided to parse json "by the hands". And I can't figure out how to parse nested arrays in array, there is my json:

[
   {
      "title":"Phone",
      "subs":[
         {
            "id":157291,
            "title":"Cell Phone Service"
         },
         {
            "id":524624,
            "title":"Landline Phone Service"
         },
         {
            "id":157298,
            "title":"Voice Over IP"
         }
      ]
   },
   {
      "title":"TV and Internet",
      "subs":[
         {
            "id":157292,
            "title":"Hardwire Internet"
         },
         {
            "id":178472,
            "title":"Television"
         },
         {
            "id":524625,
            "title":"Wireless Internet"
         }
      ]
   },
   {
      "title":"Entertainment",
      "subs":[
         {
            "id":522695,
            "title":"Music and Movies"
         }
      ]
   },
   {
      "title":"Bill Payment",
      "subs":[
         {
            "id":179845,
            "title":"Home Utilities"
         }
      ]
   },
   {
      "title":"Games and Social",
      "subs":[
         {
            "id":157297,
            "title":"Games",
            "subs":[
               {
                  "id":525000,
                  "title":"Category:Casual"
               },
               {
                  "id":525001,
                  "title":"Category:Online Games"
               },
               {
                  "id":525002,
                  "title":"Category:Action and Shooter Games"
               },
               {
                  "id":525003,
                  "title":"Category:RPG"
               },
               {
                  "id":525005,
                  "title":"Category:Strategy"
               },
               {
                  "id":525006,
                  "title":"Category:Adventure"
               },
               {
                  "id":525008,
                  "title":"Category:Simulators"
               },
               {
                  "id":525171,
                  "title":"Category:Portals and Services"
               },
               {
                  "id":525265,
                  "title":"Category:Game artefacts"
               }
            ]
         },
         {
            "id":524626,
            "title":"Social Networks"
         },
         {
            "id":522901,
            "title":"Dating"
         }
      ]
   },
   {
      "title":"Finances",
      "subs":[
         {
            "id":522747,
            "title":"Loan Repayment"
         }
      ]
   },
   {
      "title":"Everyday Purchases",
      "subs":[
         {
            "id":288993,
            "title":"Beauty, Health, and Sports"
         }
      ]
   },
   {
      "title":"Travel",
      "subs":[
         {
            "id":523297,
            "title":"Travel Reservations"
         },
         {
            "id":524634,
            "title":"Travel Agencies"
         }
      ]
   },
   {
      "title":"Websites",
      "subs":[
         {
            "id":160550,
            "title":"Advertising"
         },
         {
            "id":233554,
            "title":"Domain Hosting"
         }
      ]
   },
   {
      "title":"And also:",
      "subs":[
         {
            "id":179843,
            "title":"Charity"
         },
         {
            "id":524635,
            "title":"Online auctions"
         },
         {
            "id":522887,
            "title":"Miscellaneous"
         }
      ]
   }
]

I've got the second-level nested array, using this piece of code, but how can I get the third-level array?

    String response = RequestManager.makeRequest();
    StringBuilder sbResponse = new StringBuilder();

    try {
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONArray nestedArray = jsonObject.getJSONArray("subs");
            for (int j = 0; j < nestedArray.length(); j++) {

            }
        }
        Log.d(LOG_TAG, sbResponse.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

And, by the way, how should I store this data in the database - I'm using Realm and I've created Category and SubCategory models, do I need to create another subcategory model for saving data from the third-level array?

Categoty model:

public class Category extends RealmObject {

    @PrimaryKey
    private String title;
    private SubCategory subCategory;

    //Getters and setters
}

And SubCategory model:

public class SubCategory extends RealmObject {

    @PrimaryKey
    private int id;
    private String title;

    //Getters and setters
}
3
  • subs.subs maybe? I'm not sure though Commented Aug 27, 2015 at 8:51
  • I've tried something like this: for (int j = 0; j < nestedArray.length(); j++) { JSONObject tlObj = nestedArray.getJSONObject(j); JSONArray tlArray = tlObj.getJSONArray("subs.subs"); sbResponse.append(tlArray.toString()); } Commented Aug 27, 2015 at 8:58
  • And got this: W/System.err﹕ org.json.JSONException: No value for subs.subs Commented Aug 27, 2015 at 9:00

2 Answers 2

1
String response = RequestManager.makeRequest();
    StringBuilder sbResponse = new StringBuilder();

    try {
        JSONArray jsonArray = new JSONArray(response);
        for (int i = 0; i < jsonArray.length(); i++) {
            Category c=new Category();
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            c.setTitle(jsonObject.getString("title"));
            JSONArray nestedArray = jsonObject.getJSONArray("subs");
            for (int j = 0; j < nestedArray.length(); j++) {
                 SubCategory s=new SubCategory();
                 JSONObject nestedObject= nestedArray.getJSONObject(i);
                 s.setId(nestedObject.getString("id"));
                 s.setTitle(nestedObject.getString("title"));
            }
            c.setSubCategory(s);
        }
        Log.d(LOG_TAG, sbResponse.toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

Comments

0
try {
    JSONArray jsonArray = new JSONArray(response);
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        JSONArray nestedArray = jsonObject.getJSONArray("subs");
        for (int j = 0; j < nestedArray.length(); j++) {
            JSONObject nestObj = nestedArray.getJSONObject(j);
            if(nestObj.has("subs"))
            {
                JSONArray insideArray = nestObj.getJSONArray("subs");
                 for (int k = 0; k < insideArray .length(); k++) {

                 }
            }
        }
    }
    Log.d(LOG_TAG, sbResponse.toString());
} catch (JSONException e) {
    e.printStackTrace();
}

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.