好吧,所以我正在尝试从Weather Underground的一个JSON文件输出一些Android应用程序的数据。我需要的是当前的温度和当天的条件,接下来的10天。提供的JSON数据给出了10天的展望,但我不知道如何分别解析每一天。而且,我甚至无法将温度和条件变为单个字符串输出。
这是JSON集:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"forecast10day": 1
}
}
,
"forecast":{
"txt_forecast": {
"date":"1:27 PM CDT",
"forecastday": [
{
"period":0,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Wednesday",
"fcttext":"A mix of clouds and sun. Slight chance of a rain shower. High around 60F. Winds SSE at 10 to 20 mph.",
"fcttext_metric":"Sun and clouds mixed. Slight chance of a rain shower. High near 15C. Winds SSE at 15 to 30 km/h.",
"pop":"10"
}
,
{
"period":1,
"icon":"nt_rain",
"icon_url":"http://icons.wxug.com/i/c/k/nt_rain.gif",
"title":"Wednesday Night",
"fcttext":"Showers and thundershowers this evening will give way to steady rain overnight. Low 51F. Winds S at 10 to 15 mph. Chance of rain 70%.",
"fcttext_metric":"Showers and thundershowers this evening, then cloudy with rain likely overnight. Low around 10C. Winds S at 15 to 25 km/h. Chance of rain 70%.",
"pop":"70"
}
,
{
"period":2,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Thursday",
"fcttext":"Morning clouds will give way to sunshine for the afternoon. High around 70F. Winds WSW at 5 to 10 mph.",
"fcttext_metric":"Morning clouds will give way to sunshine for the afternoon. High 21C. Winds WSW at 10 to 15 km/h.",
"pop":"20"
}
,
{
"period":3,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"Thursday Night",
"fcttext":"A mostly clear sky. Low around 45F. Winds light and variable.",
"fcttext_metric":"Mostly clear. Low 7C. Winds light and variable.",
"pop":"0"
}
,
{
"period":4,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Friday",
"fcttext":"Partly cloudy. High 71F. Winds SSW at 5 to 10 mph.",
"fcttext_metric":"Intervals of clouds and sunshine. High 22C. Winds SSW at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":5,
"icon":"nt_partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
"title":"Friday Night",
"fcttext":"A few clouds. Low 49F. Winds W at 10 to 15 mph.",
"fcttext_metric":"A few clouds. Low 9C. Winds W at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":6,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"Saturday",
"fcttext":"Sunny. High near 65F. Winds WNW at 10 to 20 mph.",
"fcttext_metric":"Mainly sunny. High 18C. Winds WNW at 15 to 30 km/h.",
"pop":"0"
}
,
{
"period":7,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"Saturday Night",
"fcttext":"A mostly clear sky. Low 42F. Winds WNW at 5 to 10 mph.",
"fcttext_metric":"Clear skies. Low 6C. Winds WNW at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":8,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"Sunday",
"fcttext":"Mostly sunny skies. High 63F. Winds SE at 10 to 15 mph.",
"fcttext_metric":"Generally sunny despite a few afternoon clouds. High 17C. Winds SE at 15 to 25 km/h.",
"pop":"10"
}
,
{
"period":9,
"icon":"nt_chancerain",
"icon_url":"http://icons.wxug.com/i/c/k/nt_chancerain.gif",
"title":"Sunday Night",
"fcttext":"Partly cloudy skies early followed by increasing clouds with showers developing later at night. Low 51F. Winds SE at 10 to 15 mph. Chance of rain 40%.",
"fcttext_metric":"Partly cloudy skies early followed by increasing clouds with showers developing later at night. Low around 10C. Winds SE at 10 to 15 km/h. Chance of rain 40%.",
"pop":"40"
}
,
{
"period":10,
"icon":"chancerain",
"icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif",
"title":"Monday",
"fcttext":"Showers early becoming less numerous later in the day. High 62F. Winds WSW at 5 to 10 mph. Chance of rain 40%.",
"fcttext_metric":"Chance of a shower or two during the morning, followed by partly cloudy skies in the afternoon. High 17C. Winds WSW at 10 to 15 km/h. Chance of rain 30%.",
"pop":"40"
}
,
{
"period":11,
"icon":"nt_chancerain",
"icon_url":"http://icons.wxug.com/i/c/k/nt_chancerain.gif",
"title":"Monday Night",
"fcttext":"A shower or two around the area in the evening, then partly cloudy overnight. Low 47F. Winds WNW at 10 to 15 mph. Chance of rain 30%.",
"fcttext_metric":"A few clouds. Low 8C. Winds WNW at 15 to 25 km/h.",
"pop":"30"
}
,
{
"period":12,
"icon":"mostlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/mostlycloudy.gif",
"title":"Tuesday",
"fcttext":"Partly cloudy skies during the morning hours will become overcast in the afternoon. High 52F. Winds WNW at 15 to 25 mph.",
"fcttext_metric":"Partly to mostly cloudy. High 11C. Winds WNW at 25 to 40 km/h.",
"pop":"10"
}
,
{
"period":13,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"Tuesday Night",
"fcttext":"A mostly clear sky. Low 37F. Winds W at 5 to 10 mph.",
"fcttext_metric":"A mostly clear sky. Low 3C. Winds W at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":14,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"Wednesday",
"fcttext":"Generally sunny despite a few afternoon clouds. High 53F. Winds WSW at 5 to 10 mph.",
"fcttext_metric":"Generally sunny despite a few afternoon clouds. High 12C. Winds WSW at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":15,
"icon":"nt_partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
"title":"Wednesday Night",
"fcttext":"Partly cloudy skies. Low 37F. Winds W at 5 to 10 mph.",
"fcttext_metric":"A few clouds. Low 3C. Winds W at 10 to 15 km/h.",
"pop":"20"
}
,
{
"period":16,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Thursday",
"fcttext":"Intervals of clouds and sunshine. High 52F. Winds NNW at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy skies. High 11C. Winds NNW at 10 to 15 km/h.",
"pop":"20"
}
,
{
"period":17,
"icon":"nt_clear",
"icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif",
"title":"Thursday Night",
"fcttext":"A few clouds from time to time. Low 37F. Winds NNW at 5 to 10 mph.",
"fcttext_metric":"Mostly clear skies. Low 3C. Winds NNW at 10 to 15 km/h.",
"pop":"20"
}
,
{
"period":18,
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"title":"Friday",
"fcttext":"Except for a few afternoon clouds, mainly sunny. High 48F. Winds NE at 5 to 10 mph.",
"fcttext_metric":"Generally sunny despite a few afternoon clouds. High 9C. Winds NE at 10 to 15 km/h.",
"pop":"10"
}
,
{
"period":19,
"icon":"nt_partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
"title":"Friday Night",
"fcttext":"A few clouds from time to time. Low 36F. Winds SE at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy skies. Low 2C. Winds SE at 10 to 15 km/h.",
"pop":"10"
}
]
},
"simpleforecast": {
"forecastday": [
{"date":{
"epoch":"1414022400",
"pretty":"7:00 PM CDT on October 22, 2014",
"day":22,
"month":10,
"year":2014,
"yday":294,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Wed",
"weekday":"Wednesday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":1,
"high": {
"fahrenheit":"60",
"celsius":"16"
},
"low": {
"fahrenheit":"51",
"celsius":"11"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.19,
"mm": 5
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.19,
"mm": 5
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 20,
"kph": 32,
"dir": "SSE",
"degrees": 151
},
"avewind": {
"mph": 15,
"kph": 24,
"dir": "SSE",
"degrees": 151
},
"avehumidity": 56,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414108800",
"pretty":"7:00 PM CDT on October 23, 2014",
"day":23,
"month":10,
"year":2014,
"yday":295,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Thu",
"weekday":"Thursday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":2,
"high": {
"fahrenheit":"70",
"celsius":"21"
},
"low": {
"fahrenheit":"45",
"celsius":"7"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"skyicon":"",
"pop":20,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "WSW",
"degrees": 246
},
"avewind": {
"mph": 7,
"kph": 11,
"dir": "WSW",
"degrees": 246
},
"avehumidity": 69,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414195200",
"pretty":"7:00 PM CDT on October 24, 2014",
"day":24,
"month":10,
"year":2014,
"yday":296,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Fri",
"weekday":"Friday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":3,
"high": {
"fahrenheit":"71",
"celsius":"22"
},
"low": {
"fahrenheit":"49",
"celsius":"9"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"skyicon":"",
"pop":0,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "SSW",
"degrees": 209
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "SSW",
"degrees": 209
},
"avehumidity": 64,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414281600",
"pretty":"7:00 PM CDT on October 25, 2014",
"day":25,
"month":10,
"year":2014,
"yday":297,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Sat",
"weekday":"Saturday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":4,
"high": {
"fahrenheit":"65",
"celsius":"18"
},
"low": {
"fahrenheit":"42",
"celsius":"6"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":0,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 20,
"kph": 32,
"dir": "WNW",
"degrees": 298
},
"avewind": {
"mph": 16,
"kph": 26,
"dir": "WNW",
"degrees": 298
},
"avehumidity": 42,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414368000",
"pretty":"7:00 PM CDT on October 26, 2014",
"day":26,
"month":10,
"year":2014,
"yday":298,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Sun",
"weekday":"Sunday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":5,
"high": {
"fahrenheit":"63",
"celsius":"17"
},
"low": {
"fahrenheit":"51",
"celsius":"11"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.03,
"mm": 1
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.03,
"mm": 1
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 15,
"kph": 24,
"dir": "SE",
"degrees": 129
},
"avewind": {
"mph": 10,
"kph": 16,
"dir": "SE",
"degrees": 129
},
"avehumidity": 50,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414454400",
"pretty":"7:00 PM CDT on October 27, 2014",
"day":27,
"month":10,
"year":2014,
"yday":299,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Mon",
"weekday":"Monday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":6,
"high": {
"fahrenheit":"62",
"celsius":"17"
},
"low": {
"fahrenheit":"47",
"celsius":"8"
},
"conditions":"Chance of Rain",
"icon":"chancerain",
"icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif",
"skyicon":"",
"pop":40,
"qpf_allday": {
"in": 0.03,
"mm": 1
},
"qpf_day": {
"in": 0.03,
"mm": 1
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "WSW",
"degrees": 237
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "WSW",
"degrees": 237
},
"avehumidity": 75,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414540800",
"pretty":"7:00 PM CDT on October 28, 2014",
"day":28,
"month":10,
"year":2014,
"yday":300,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Tue",
"weekday":"Tuesday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":7,
"high": {
"fahrenheit":"52",
"celsius":"11"
},
"low": {
"fahrenheit":"37",
"celsius":"3"
},
"conditions":"Mostly Cloudy",
"icon":"mostlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/mostlycloudy.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 25,
"kph": 40,
"dir": "WNW",
"degrees": 301
},
"avewind": {
"mph": 18,
"kph": 29,
"dir": "WNW",
"degrees": 301
},
"avehumidity": 54,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414627200",
"pretty":"7:00 PM CDT on October 29, 2014",
"day":29,
"month":10,
"year":2014,
"yday":301,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Wed",
"weekday":"Wednesday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":8,
"high": {
"fahrenheit":"53",
"celsius":"12"
},
"low": {
"fahrenheit":"37",
"celsius":"3"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "WSW",
"degrees": 239
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "WSW",
"degrees": 239
},
"avehumidity": 57,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414713600",
"pretty":"7:00 PM CDT on October 30, 2014",
"day":30,
"month":10,
"year":2014,
"yday":302,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Thu",
"weekday":"Thursday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":9,
"high": {
"fahrenheit":"52",
"celsius":"11"
},
"low": {
"fahrenheit":"37",
"celsius":"3"
},
"conditions":"Partly Cloudy",
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"skyicon":"",
"pop":20,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "NNW",
"degrees": 340
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "NNW",
"degrees": 340
},
"avehumidity": 69,
"maxhumidity": 0,
"minhumidity": 0
}
,
{"date":{
"epoch":"1414800000",
"pretty":"7:00 PM CDT on October 31, 2014",
"day":31,
"month":10,
"year":2014,
"yday":303,
"hour":19,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"October",
"monthname_short":"Oct",
"weekday_short":"Fri",
"weekday":"Friday",
"ampm":"PM",
"tz_short":"CDT",
"tz_long":"America/Chicago"
},
"period":10,
"high": {
"fahrenheit":"48",
"celsius":"9"
},
"low": {
"fahrenheit":"36",
"celsius":"2"
},
"conditions":"Clear",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"skyicon":"",
"pop":10,
"qpf_allday": {
"in": 0.00,
"mm": 0
},
"qpf_day": {
"in": 0.00,
"mm": 0
},
"qpf_night": {
"in": 0.00,
"mm": 0
},
"snow_allday": {
"in": 0.0,
"cm": 0.0
},
"snow_day": {
"in": 0.0,
"cm": 0.0
},
"snow_night": {
"in": 0.0,
"cm": 0.0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "NE",
"degrees": 43
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "NE",
"degrees": 43
},
"avehumidity": 66,
"maxhumidity": 0,
"minhumidity": 0
}
]
}
}
}
到目前为止,这是我的代码(第一天片段)
class RequestCurrentMplsWeatherTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri)
{
String responseString = null;
try {
HttpClient client = new DefaultHttpClient();
URI apiCall = new URI("http://api.wunderground.com/api/MYID/forecast10day/q/MN/Minneapolis.json");
HttpGet request = new HttpGet();
request.setURI(apiCall);
HttpResponse response = client.execute(request);
responseString = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
Log.e(TAG, "some sort of problem encountered", e);
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null ){
Log.i(TAG, "result data was \n" + result);
try {
JSONObject jsonWeather = new JSONObject(result);
JSONObject weatherForecast = jsonWeather.getJSONObject("forecast");
JSONObject simpleForecast = weatherForecast.getJSONObject("simpleforecast");
JSONArray forecastArray = simpleForecast.getJSONArray("forecastday");
for (int i = 0; i < forecastArray.length(); i++) {
JSONObject fa = forecastArray.getJSONObject(i);
JSONArray highArray = fa.getJSONArray("high");
for (int h = 0; h < highArray.length(); h++) {
JSONObject ha = highArray.getJSONObject(h);
JSONObject fahrenheit = ha.getJSONObject("fahrenheit");
TextView output1 = (TextView)findViewById(R.id.today_temp);
output1.setText("" + fahrenheit);
break;
}
JSONObject conditions = fa.getJSONObject("conditions");
//JSONObject conditionsArray = fa.getJSONObject("conditions");
TextView output2 = (TextView)findViewById(R.id.fctext);
output2.setText("" + conditions);
break;
}
//String forecastArrayResult = weatherForecast.getString("temp_f");
} catch (JSONException jse) {
Log.e(TAG, "Error parsing JSON", jse);
return;
}
} else {
Log.i(TAG, "String returned was Null, check doInBackground for errors");
}
}
}
我认为获得每天预测的最佳方法可能是在JSON集上运行一个循环,然后打印出与每天相对应的“句号”。但是,我无法弄清楚如何将上述温度和预测数据打印到带有JSON的for循环设置的参数中。
如果有人能帮助我弄清楚如何至少获得当天的预测,我会很高兴。但如果我能以任何其他方式开悟,请告诉我。感谢。
答案 0 :(得分:2)
我看到错误,你试图获取JsonArray而不是JsonObject,以验证你可以使用下一页来查看你的json http://www.jsoneditoronline.org/
您更改了代码。这是我的代码。
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result != null) {
Log.i(TAG, "result data was \n" + result);
try {
JSONObject jsonWeather = new JSONObject(result);
JSONObject weatherForecast = jsonWeather
.getJSONObject("forecast");
JSONObject simpleForecast = weatherForecast
.getJSONObject("simpleforecast");
JSONArray forecastArray = simpleForecast
.getJSONArray("forecastday");
for (int i = 0; i < forecastArray.length(); i++) {
JSONObject fa = forecastArray.getJSONObject(i);
JSONObject highArray = fa.getJSONObject("high");
//for (int h = 0; h < highArray.length(); h++) {
String fahrenheit = highArray.getString("fahrenheit");
//JSONObject fahrenheit = ha.getJSONObject("fahrenheit");
Log.v(TAG, "-fa-" + fahrenheit);
//break;
//}
String conditions = fa.getString("conditions");
// JSONObject conditionsArray =
// fa.getJSONObject("conditions");
// TextView output2 = (TextView)findViewById(R.id.fctext);
// output2.setText("" + conditions);
Log.v(TAG, "-Con-" + conditions);
break;
}
// String forecastArrayResult =
// weatherForecast.getString("temp_f");
} catch (JSONException jse) {
Log.e(TAG, "Error parsing JSON", jse);
return;
}
} else {
Log.i(TAG,
"String returned was Null, check doInBackground for errors");
}
}