这是我从OpenWeatherMap收到的JSON响应:
{
"coord": {
"lon": 85.84,
"lat": 20.26
},
"weather": [
{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 305.15,
"pressure": 1007,
"humidity": 70,
"temp_min": 305.15,
"temp_max": 305.15
},
"visibility": 5000,
"wind": {
"speed": 4.1,
"deg": 170
},
"clouds": {
"all": 20
},
"dt": 1551686400,
"sys": {
"type": 1,
"id": 9113,
"message": 0.0037,
"country": "IN",
"sunrise": 1551659668,
"sunset": 1551702153
},
"id": 1275817,
"name": "Bhubaneswar",
"cod": 200
}
虽然我可以通过下面的代码使用Weather,但无法将main转换为array。无论如何,我可以将加粗的响应转换为数组或字符串。
JSONObject jsonObject = new JSONObject(result);
String weatherInfo = jsonObject.getString("weather");
JSONArray arr = new JSONArray(weatherInfo);
for(int i=0;i<arr.length();i++)
{
JSONObject jsonPart = arr.getJSONObject(i);
String main = jsonPart.getString("main");
String description =jsonPart.getString("description");
if(main !="" && description !="")
{
message += main + " : " + description + "\r\n";
}
}