0

If my JSON object looks something like this:

{
 "weather:{
   "sunny": "yes"
   "wind": "48mph"
   "location":{
     "city": "new york"
     "zip": "12345"
   }
 }
 "rating": "four stars"
}

How would I access the city name? I can use optString to get all of "weather" or "rating" but how do I get info that's inside that?

0

3 Answers 3

3

It's very simple

JSONObject jsonObj = new JSONObject(jsonString);
JSONObject weather = jsonObj.getJSONObject("weather");
JSONObject location = weather.getJSONObject("location");
String city = location.getString("city");

Read up on JSONObject

Sign up to request clarification or add additional context in comments.

Comments

0
JSONObject json = new JSONObject(yourdata); 
JSONObject weather = json.getString("weather");
weather.getString("sunny"); //yes
weather.getString("wind"); //46mph

JSONObject location = new JSONObject(weather.getString("location"));
location.getString("city"); // new york

json.getString("rating"); //... 

Comments

0
JSONObject obj = new JSONObject(jsonString);
JSONObject weatherObj = obj.getJSONObject("weather");
JSONObject locationObj = weatherObj.getJSONObject("location");
String city = locationObj.getString("city"); //new york

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.