如何在Android中使用java访问嵌套的JSON数据?
问题描述:
如果我的JSON对象看起来是这样的:如何在Android中使用java访问嵌套的JSON数据?
{
"weather:{
"sunny": "yes"
"wind": "48mph"
"location":{
"city": "new york"
"zip": "12345"
}
}
"rating": "four stars"
}
我将如何访问这个城市叫什么名字?我可以使用optString来获取所有“天气”或“评级”,但是如何获取其中的信息?
答
这很简单
JSONObject jsonObj = new JSONObject(jsonString);
JSONObject weather = jsonObj.getJSONObject("weather");
JSONObject location = weather.getJSONObject("location");
String city = location.getString("city");
阅读上JSONObject
答
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"); //...
答
JSONObject obj = new JSONObject(jsonString);
JSONObject weatherObj = obj.getJSONObject("weather");
JSONObject locationObj = weatherObj.getJSONObject("location");
String city = locationObj.getString("city"); //new york
http://developer.android.com/reference/org/json/JSONObject.html – Robert
@Robert感谢罗伯特我加了链接 – meda