Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

BlogReader: I have a JSON file with multiple arrays in it. How do I access the information?

So in the tutorials we were taught how to handle single arrays in the JSON file. In the teamtreehouse blog json the array looked like this:

{

  "status": "ok",

  "count": 20,

  "count_total": 1764,

  "pages": 89,

  "posts": [

        {

              "id": 23705,

              "url": "http://blog.teamtreehouse.com/new-course-wordpress-user-roles",

              "title": "Getting to Grips with WordPress User Roles",

              "date": "2014-06-19 17:51:18",

              "author": "Zac Gordon",

              "thumbnail": "http://blog.teamtreehouse.com/wp-content/uploads/2014/06/pic-of-zac-150x150.png"
         },

In the JSON I am trying to use it looks like this:

{

 "kind": "Listing",

 "data": {

      "modhash": "0h1lsv6zgi590b5c3d88f5798f09f2f1652fd34b06acc2a567",

      "children": [

           {

                "kind": "t3",

                "data": {

                     "domain": "google.com"

           }

My question is: How do I get access to "domain"? Thanks.

1 Answer

Hey! I didn't test it but it should be something like this, try to understand what i've done.

... 
mBlogData
...

JSONObject jsonData= mBlogData.getJSONObject("data");
JSONArray childrenArray = jsonData.getJSONArray("children");

for(int i = 0; i < childrenArray.lenght(); i++) {
      JSONObject childrenData = childrenArray.getJSONObject(i);
      JSONObject data = childrenData.getJSONObject("data");
      String domain = data.getString("domain");
}