/ / Wie kann ich mit Java auf verschachtelte JSON-Daten in Android zugreifen? - Java, Android, Json

Wie greife ich auf verschachtelte JSON-Daten in Android mit Java zu? - Java, Android, JSON

Wenn mein JSON-Objekt ungefähr so ​​aussieht:

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

Wie kann ich auf den Namen der Stadt zugreifen? Ich kann optString verwenden, um alle "Wetter" oder "Bewertungen" zu erhalten.

Antworten:

3 für die Antwort № 1

Es ist sehr einfach

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

Lesen Sie weiter JSON-Objekt


0 für die Antwort № 2
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"); //...

0 für die Antwort № 3
JSONObject obj = new JSONObject(jsonString);
JSONObject weatherObj = obj.getJSONObject("weather");
JSONObject locationObj = weatherObj.getJSONObject("location");
String city = locationObj.getString("city"); //new york