我得到的所有资源都是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");
3 回答
![?](http://img1.sycdn.imooc.com/533e564d0001308602000200-100-100.jpg)
慕容3067478
TA贡献1773条经验 获得超3个赞
假设您使用的是 Android(在这里适当地标记有帮助,因为它取决于您使用的 JSON 库):
既然你有JSONObject main = jsonObject.getJSONObject("main");
,那么你应该能够做到double temp = main.getDouble("temp");
让该main
物体的温度下降。
如果你看看文档为JSONObject的,你可以看到各种各样的用于获取JSON“原始”字段的方法,比如int
,String
等-你需要使用这些检索说原始的领域,而不是调用getJSONObject()
。
![?](http://img1.sycdn.imooc.com/54584dd900014f6c02200220-100-100.jpg)
红颜莎娜
TA贡献1842条经验 获得超12个赞
temp 不是 JSonObject。你可以得到字符串或双倍的温度。因此,将代码的最后一行更改为:
String temp = main.getString("temp");
或者
double temp = main.getDouble("temp");
添加回答
举报
0/150
提交
取消