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

Don't understand what it is I am doing wrong with jsonData object...

I am working on a challenge to create String variables named them, and then set them to the data from a jsonData file. Well I believe I have done that, but I see to be having some issues.

JSONChallenge.java
JSONObject object = new JSONObject();

String name = object.getString("Treehouse Book Series");
String publisher = object.getString("Wiley");
String language = object.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
        }
    ]
}

Hi , I think you're getting the wrong data, instead for example the first line should be : String name = object.getString("name"); then it returns "Treehouse Book Series". likewise , String publisher = object.getString("publisher"); . your code is currently getting the value not the variable name . try the example I just gave you and get back to me if it doesnt work.

3 Answers

Hi got your answer, if you read the question again it says the jason data is already loaded into the object jsonData, so all you have to do is this: //for clarity String name; String publisher; String language;

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

I went back to do this one and it worked , try it

Oh wow, I did exactly that one time around and instead of jsonData.getString I put object.getString having declared JSONObject object = new JSONObject();

So thank you. I just got confused on what it meant by the instructions. It may seem super clear and upfront but I unfortunately have trouble understanding some things right away so thank you!

It's alright mate , we all learning, I get confused a lot too..

That doesn't work..

Code is below

// A JSONObject variable named 'jsonData'
// was loaded from the data.json file.
JSONObject object = new JSONObject();

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

I get a Syntax error.

evening adding jsonData into the parameters of JSONObject(); didn't work either still has syntax errors. I followed the video, it's honestly really confusing when I understand why TreeHouse is wanting me to show my skills, but I personally get thrown off when a different senario is used, because I don't understand what is really being needed, and sometimes the challenge questions are vague and I have to end up revisiting the challenge numerous times before I understand it.

// A JSONObject variable named 'jsonData'
// was loaded from the data.json file.
JSONObject object = new JSONObject();

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

I have tried many different options here. I can't take away object from .getString So I am still confused. I will contact Ben on twitter and see if he could be of some help!

I apologize for constant need of help Ben! haha.