从 JSONObject 父级获取 JSONObject

我得到的所有资源都是JSONObject从JSONArray. 但在这种情况下,我想获得“温度”的温度。我不认为它是数组的一部分。我怎么能得到那个?


{  

   "coord":{  

      "lon":85.17,

      "lat":26.67

   },

   "weather":[  

      {  

         "id":500,

         "main":"Rain",

         "description":"light rain",

         "icon":"10d"

      }

   ],

   "base":"stations",

   "main":{  

      "temp":31.09,

      "pressure":1004.15,

      "humidity":83,

      "temp_min":31.09,

      "temp_max":31.09,

      "sea_level":1010.39,

      "grnd_level":1004.15

   },

   "wind":{  

      "speed":3.66,

      "deg":107.5

   },

   "rain":{  

      "3h":0.202

   },

   "clouds":{  

      "all":64

   },

   "dt":1534148929,

   "sys":{  

      "message":0.0048,

      "country":"IN",

      "sunrise":1534117810,

      "sunset":1534165068

   },

   "id":1273043,

   "name":"Dhaka",

   "cod":200

}

我已经尝试过-(我是 JSON 新手)


JSONObject jsonObject = new JSONObject(s);

JSONObject main = jsonObject.getJSONObject("main");

JSONObject temp = main.getJSONObject("temp");


米脂
浏览 233回答 3
3回答

慕容3067478

假设您使用的是 Android(在这里适当地标记有帮助,因为它取决于您使用的 JSON 库):既然你有JSONObject main = jsonObject.getJSONObject("main");,那么你应该能够做到double temp = main.getDouble("temp");让该main物体的温度下降。如果你看看文档为JSONObject的,你可以看到各种各样的用于获取JSON“原始”字段的方法,比如int,String等-你需要使用这些检索说原始的领域,而不是调用getJSONObject()。

红颜莎娜

temp 不是 JSonObject。你可以得到字符串或双倍的温度。因此,将代码的最后一行更改为:String temp = main.getString("temp");或者double temp = main.getDouble("temp");

慕码人8056858

main使用“temp”作为键从对象中获取值double temp = main.getDouble("temp");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java