Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java访问android中的嵌套JSON数据?_Java_Android_Json - Fatal编程技术网

如何使用java访问android中的嵌套JSON数据?

如何使用java访问android中的嵌套JSON数据?,java,android,json,Java,Android,Json,如果我的JSON对象看起来像这样: { "weather:{ "sunny": "yes" "wind": "48mph" "location":{ "city": "new york" "zip": "12345" } } "rating": "four stars" } 如何访问城市名称?我可以使用optString获取所有的“天气”或“评级”,但如何获取其中的信息呢?非常简单 JSONObject json = new JSONObjec

如果我的JSON对象看起来像这样:

{
 "weather:{
   "sunny": "yes"
   "wind": "48mph"
   "location":{
     "city": "new york"
     "zip": "12345"
   }
 }
 "rating": "four stars"
}
如何访问城市名称?我可以使用optString获取所有的“天气”或“评级”,但如何获取其中的信息呢?

非常简单

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 jsonObj = new JSONObject(jsonString);
JSONObject weather = jsonObj.getJSONObject("weather");
JSONObject location = weather.getJSONObject("location");
String city = location.getString("city");
请仔细阅读

这很简单

JSONObject jsonObj = new JSONObject(jsonString);
JSONObject weather = jsonObj.getJSONObject("weather");
JSONObject location = weather.getJSONObject("location");
String city = location.getString("city");
请仔细阅读

这很简单

JSONObject jsonObj = new JSONObject(jsonString);
JSONObject weather = jsonObj.getJSONObject("weather");
JSONObject location = weather.getJSONObject("location");
String city = location.getString("city");
请仔细阅读

这很简单

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