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

标签 java android json

如果我的 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

关于java - 如何使用 java 访问 android 中的嵌套 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24917509/

相关文章:

android - Retrofit 2 拦截器具有私有(private)访问权限

android - 类重复在 Android 仪器测试中添加 Robolectric

java - Stream.map 和 Collectors.mapping 之间的性能差异

java - PrimeFaces GMap 忽略模型标签,不显示标记

java - android – 在有限的时间内显示一个按钮

json - 如何处理 JSON 中的多对多关系?

java - 使用 Gson 将 JSON 数组解析为 Java 列表

python - 使用 sqlalchemy 在 SQLite 中创建 JSON 类型列

java - 如何允许用户在 Android 上使用 Facebook SDK FeedDialogBu​​ilder 更改帖子的目标受众

java - 从 SAX 或 Dom 中的 XML 读取元素内部的元素