这是我想要获取数据的整个字符串(这基本上是伦敦的天气详情)
{
"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();
}
}
使用上面的代码,我可以从&#34;天气&#34;中获取主要和描述等数据。数组但我无法从&#34; sys&#34;中获取数据。数组(日落,日出值和国家)
答案 0 :(得分:1)
“sys”不是数组,可以从代码中的根json对象“jsonObject”访问它。
{{1}}