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 Weather App (2015) Working with JSON Using JSONObject

JSONObject objective I am not able to run properly.

JSONObject jsonData = new JSONObject(data.json); String name = jsonData.getString("Treehouse Book Series"); String publisher= jsonData.getString("Wiley"); String language = jsonData.getString("English"); this is the code I have written in editor Help please.

JSONChallenge.java
// A JSONObject variable named 'jsonData'
// was loaded from the data.json file.

JSONObject jsonData = new JSONObject(data.json);
String name = jsonData.getString("Treehouse Book Series");
String publisher= jsonData.getString("Wiley");
String language = jsonData.getString("English");
data.json
{
    "name":"Treehouse Book Series",
    "publisher":"Wiley",
    "language":"English",
    "books":[
        {
            "title":"HTML5 Foundations",
            "author":"Matt West",
            "pages":384
        },
        {
            "title":"CSS3 Foundations",
            "author":"Ian Lunn",
            "pages":352
        }
    ]
}

1 Answer

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Niravkumar,

I'll post your code first for reference:

// A JSONObject variable named 'jsonData'
// was loaded from the data.json file.

JSONObject jsonData = new JSONObject(data.json);
String name = jsonData.getString("Treehouse Book Series");
String publisher= jsonData.getString("Wiley");
String language = jsonData.getString("English");

The comments in this method says that your JSONObject is already referenced in a variable jsonData. We do not need to make the JSONObject (let's get rid of the first line).

// A JSONObject variable named 'jsonData'
// was loaded from the data.json file.

JSONObject jsonData = new JSONObject(data.json);
String name = jsonData.getString("Treehouse Book Series");
String publisher= jsonData.getString("Wiley");
String language = jsonData.getString("English");

The idea behind JSON is that we can get data via key-value pairs. We may not know in advance what the "name" value corresponds to, we just want whatever is inside "name" When we call the getString() method, we give it they key we want, not the value that we expect.

// A JSONObject variable named 'jsonData'
// was loaded from the data.json file.

String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String  language = jsonData.getString("language");

In the first line, we call "getString("name") on the right side of '=' so that we will have ""Treehouse Book Series"" on the left side stored in the variable called 'name'

I hope this helps.