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 Build a Blog Reader Android App Getting Data from the Web Parsing Data Returned in JSON Format

Stephen Amaza
Stephen Amaza
1,549 Points

What does this line mean?

What does this line of code mean?

JSONObject book = jsonBooks.getJSONObject(i);

This is from a challenge

1 Answer

Dan Johnson
Dan Johnson
40,533 Points

With jsonBooks defined as:

JSONArray jsonBooks = jsonData.getJSONArray("books");

You could translate it as, "From the JSON array "books", give me the element at index i which is a JSON object."

{
    "name":"Treehouse Book Series",
    "publisher":"Wiley",
    "language":"English",
    //From this array
    "books":[ 
         //At i = 0
        { 
            "title":"HTML5 Foundations",
            "author":"Matt West",
            "pages":384
        },
        //At i = 1
        {
            "title":"CSS3 Foundations",
            "author":"Ian Lunn",
            "pages":352
        }
    ]
}

Note that the comments I added will cause problems in an actual JSON file.

Stephen Amaza
Stephen Amaza
1,549 Points

Awesome! Thank you. If only tech books could be this straightforward