我得到的所有资源都是从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");发布于 2018-08-13 09:33:45
假设您使用的是Android (在这里进行适当的标记会有所帮助,因为它取决于您使用的JSON库):
既然您已经得到了JSONObject main = jsonObject.getJSONObject("main");,那么您应该能够做double temp = main.getDouble("temp");来获得main对象的温度。
如果您查看JSONObject文档,您可以看到各种获取JSON“原语”字段的方法,例如int、String等--您需要使用这些方法来检索所述的原始字段,而不是调用getJSONObject()。
发布于 2018-08-13 09:34:23
使用"temp“作为键从main对象获取值
double temp = main.getDouble("temp");发布于 2018-08-13 09:34:50
temp不是JSonObject。你可以得到的温度是字符串或双倍。因此,用以下方法更改代码的最后一行:
String temp = main.getString("temp");或
double temp = main.getDouble("temp");https://stackoverflow.com/questions/51819153
复制相似问题