【发布时间】:2017-01-27 17:12:50
【问题描述】:
这是我要从中获取数据的整个字符串(这基本上是伦敦的天气详细信息)
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 721,
"main": "Haze",
"description": "haze",
"icon": "50d"
},
{
"id": 701,
"main": "Mist",
"description": "mist",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 278.15,
"pressure": 1010,
"humidity": 81,
"temp_min": 276.15,
"temp_max": 279.15
},
"visibility": 6000,
"wind": {
"speed": 4.1,
"deg": 110
},
"clouds": {
"all": 40
},
"dt": 1485525000,
"sys": {
"type": 1,
"id": 5091,
"message": 0.048,
"country": "GB",
"sunrise": 1485503103,
"sunset": 1485535344
},
"id": 2643743,
"name": "London",
"cod": 200
}
我在我的 android 代码中使用过这个
protected void onPostExecute(String result)
{
super.onPostExecute(result);
try {
String message = "";
JSONObject jsonObject = new JSONObject(result);
String weatherInfo = jsonObject.getString("weather");
Log.i("Weather content", weatherInfo);
JSONArray arr = new JSONArray(weatherInfo);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
String main = "";
String description = "";
main = jsonPart.getString("main");
description = jsonPart.getString("description");
if (main != "" && description != "") {
message += main + ": " + description + "\r\n";
}
}
if (message != "") {
resultTextView.setText(message);
} else {
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Could not find weather", Toast.LENGTH_LONG).show();
}
}
使用上面的代码,我可以从“天气”数组中获取主要和描述等数据,但我无法从“sys”数组中获取数据(对于日落、日出值和国家/地区)
【问题讨论】:
-
可能是因为sys也是主要标签。