在Java3级别深层解析JSON
提前谢谢你 我正在尝试解析来自Weather Underground api的JSON数组。10天预测的回报如下:在Java3级别深层解析JSON,java,json,Java,Json,提前谢谢你 我正在尝试解析来自Weather Underground api的JSON数组。10天预测的回报如下: { "response": { "version": "0.1", "termsofService": "http://www.wunderground.com/weather/api/d/terms.html", "features": { "forecast10day": 1 } }, "forecast": { "txt_forecast"
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"forecast10day": 1
}
},
"forecast": {
"txt_forecast": {
"date": "8:00 AM PDT",
"forecastday": [{
"period": 0,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Tuesday",
"fcttext": "Overcast in the morning, then clear. High of 75F. Winds from the SSW at 5 to 20 mph.",
"fcttext_metric": "Overcast in the morning, then clear. High of 24C. Windy. Winds from the SSW at 5 to 30 km/h.",
"pop": "0"
}, {
"period": 1,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Tuesday Night",
"fcttext": "Mostly cloudy. Low of 55F. Winds from the SW at 5 to 15 mph.",
"fcttext_metric": "Mostly cloudy. Low of 13C. Breezy. Winds from the SW at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 2,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Wednesday",
"fcttext": "Mostly cloudy in the morning, then clear. High of 72F. Winds from the SW at 10 to 15 mph.",
"fcttext_metric": "Mostly cloudy in the morning, then clear. High of 22C. Breezy. Winds from the SW at 15 to 25 km/h.",
"pop": "0"
}, {
"period": 3,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Wednesday Night",
"fcttext": "Mostly cloudy. Fog overnight. Low of 55F. Winds from the SW at 10 to 15 mph.",
"fcttext_metric": "Mostly cloudy. Fog overnight. Low of 13C. Breezy. Winds from the SW at 15 to 20 km/h.",
"pop": "0"
}, {
"period": 4,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Thursday",
"fcttext": "Mostly cloudy. Fog early. High of 70F. Winds from the WSW at 10 to 15 mph.",
"fcttext_metric": "Mostly cloudy. Fog early. High of 21C. Windy. Winds from the WSW at 15 to 25 km/h.",
"pop": "0"
}, {
"period": 5,
"icon": "clear",
"icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"title": "Thursday Night",
"fcttext": "Partly cloudy. Fog overnight. Low of 55F. Winds from the WNW at 5 to 10 mph.",
"fcttext_metric": "Partly cloudy. Fog overnight. Low of 13C. Winds from the WNW at 10 to 15 km/h.",
"pop": "0"
}, {
"period": 6,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Friday",
"fcttext": "Partly cloudy in the morning, then clear. High of 72F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Partly cloudy in the morning, then clear. High of 22C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 7,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Friday Night",
"fcttext": "Partly cloudy. Fog overnight. Low of 55F. Winds from the WNW at 5 to 10 mph.",
"fcttext_metric": "Partly cloudy. Fog overnight. Low of 13C. Winds from the WNW at 10 to 15 km/h.",
"pop": "0"
}, {
"period": 8,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Saturday",
"fcttext": "Partly cloudy. Fog early. High of 73F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Partly cloudy. Fog early. High of 23C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 9,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Saturday Night",
"fcttext": "Clear. Fog overnight. Low of 55F. Winds from the West at 5 to 10 mph.",
"fcttext_metric": "Clear. Fog overnight. Low of 13C. Winds from the West at 10 to 15 km/h.",
"pop": "0"
}, {
"period": 10,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Sunday",
"fcttext": "Partly cloudy. High of 72F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Partly cloudy. High of 22C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 11,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Sunday Night",
"fcttext": "Partly cloudy. Fog overnight. Low of 55F. Winds from the West at 5 to 10 mph.",
"fcttext_metric": "Partly cloudy. Fog overnight. Low of 13C. Winds from the West at 10 to 15 km/h.",
"pop": "0"
}, {
"period": 12,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Monday",
"fcttext": "Clear. High of 70F. Winds from the West at 5 to 10 mph.",
"fcttext_metric": "Clear. High of 21C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 13,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Monday Night",
"fcttext": "Partly cloudy. Fog overnight. Low of 55F. Winds from the West at 5 to 10 mph.",
"fcttext_metric": "Partly cloudy. Fog overnight. Low of 13C. Winds from the West at 5 to 15 km/h.",
"pop": "0"
}, {
"period": 14,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Tuesday",
"fcttext": "Clear. High of 75F. Winds from the West at 5 to 10 mph.",
"fcttext_metric": "Clear. High of 24C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 15,
"icon": "clear",
"icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"title": "Tuesday Night",
"fcttext": "Clear. Low of 59F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Clear. Low of 15C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 16,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Wednesday",
"fcttext": "Clear. High of 73F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Clear. High of 23C. Winds from the West at 5 to 20 km/h.",
"pop": "0"
}, {
"period": 17,
"icon": "clear",
"icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"title": "Wednesday Night",
"fcttext": "Clear. Low of 59F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Clear. Low of 15C. Breezy. Winds from the West at 10 to 20 km/h.",
"pop": "0"
}, {
"period": 18,
"icon": "clear",
"icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"title": "Thursday",
"fcttext": "Clear. High of 73F. Winds from the West at 5 to 15 mph.",
"fcttext_metric": "Clear. High of 23C. Winds from the West at 5 to 20 km/h.",
"pop": "0"
}, {
"period": 19,
"icon": "clear",
"icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"title": "Thursday Night",
"fcttext": "Clear. Low of 57F. Winds from the WSW at 5 to 10 mph.",
"fcttext_metric": "Clear. Low of 14C. Winds from the WSW at 10 to 15 km/h.",
"pop": "0"
}]
},
"simpleforecast": {
"forecastday": [{
"date": {
"epoch": "1341381600",
"pretty": "11:00 PM PDT on July 03, 2012",
"day": 3,
"month": 7,
"year": 2012,
"yday": 184,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Tue",
"weekday": "Tuesday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 1,
"high": {
"fahrenheit": "75",
"celsius": "24"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "mostlysunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 17,
"kph": 27,
"dir": "WSW",
"degrees": 255
},
"avewind": {
"mph": 14,
"kph": 22,
"dir": "WSW",
"degrees": 241
},
"avehumidity": 72,
"maxhumidity": 82,
"minhumidity": 62
}, {
"date": {
"epoch": "1341468000",
"pretty": "11:00 PM PDT on July 04, 2012",
"day": 4,
"month": 7,
"year": 2012,
"yday": 185,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Wed",
"weekday": "Wednesday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 2,
"high": {
"fahrenheit": "72",
"celsius": "22"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "partlycloudy",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 15,
"kph": 24,
"dir": "WSW",
"degrees": 237
},
"avewind": {
"mph": 12,
"kph": 19,
"dir": "SW",
"degrees": 231
},
"avehumidity": 79,
"maxhumidity": 89,
"minhumidity": 54
}, {
"date": {
"epoch": "1341554400",
"pretty": "11:00 PM PDT on July 05, 2012",
"day": 5,
"month": 7,
"year": 2012,
"yday": 186,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Thu",
"weekday": "Thursday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 3,
"high": {
"fahrenheit": "70",
"celsius": "21"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "mostlysunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 15,
"kph": 24,
"dir": "West",
"degrees": 265
},
"avewind": {
"mph": 13,
"kph": 21,
"dir": "WSW",
"degrees": 250
},
"avehumidity": 77,
"maxhumidity": 87,
"minhumidity": 64
}, {
"date": {
"epoch": "1341640800",
"pretty": "11:00 PM PDT on July 06, 2012",
"day": 6,
"month": 7,
"year": 2012,
"yday": 187,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Fri",
"weekday": "Friday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 4,
"high": {
"fahrenheit": "72",
"celsius": "22"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "sunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 11,
"kph": 18,
"dir": "West",
"degrees": 272
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "West",
"degrees": 274
},
"avehumidity": 70,
"maxhumidity": 84,
"minhumidity": 58
}, {
"date": {
"epoch": "1341727200",
"pretty": "11:00 PM PDT on July 07, 2012",
"day": 7,
"month": 7,
"year": 2012,
"yday": 188,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Sat",
"weekday": "Saturday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 5,
"high": {
"fahrenheit": "73",
"celsius": "23"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Fog",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "mostlysunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 11,
"kph": 18,
"dir": "West",
"degrees": 266
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "West",
"degrees": 270
},
"avehumidity": 75,
"maxhumidity": 85,
"minhumidity": 61
}, {
"date": {
"epoch": "1341813600",
"pretty": "11:00 PM PDT on July 08, 2012",
"day": 8,
"month": 7,
"year": 2012,
"yday": 189,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Sun",
"weekday": "Sunday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 6,
"high": {
"fahrenheit": "72",
"celsius": "22"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Fog",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "partlycloudy",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 11,
"kph": 18,
"dir": "West",
"degrees": 265
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "West",
"degrees": 266
},
"avehumidity": 79,
"maxhumidity": 88,
"minhumidity": 66
}, {
"date": {
"epoch": "1341900000",
"pretty": "11:00 PM PDT on July 09, 2012",
"day": 9,
"month": 7,
"year": 2012,
"yday": 190,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Mon",
"weekday": "Monday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 7,
"high": {
"fahrenheit": "70",
"celsius": "21"
},
"low": {
"fahrenheit": "55",
"celsius": "13"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "mostlysunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "West",
"degrees": 263
},
"avewind": {
"mph": 9,
"kph": 14,
"dir": "West",
"degrees": 269
},
"avehumidity": 85,
"maxhumidity": 92,
"minhumidity": 67
}, {
"date": {
"epoch": "1341986400",
"pretty": "11:00 PM PDT on July 10, 2012",
"day": 10,
"month": 7,
"year": 2012,
"yday": 191,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Tue",
"weekday": "Tuesday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 8,
"high": {
"fahrenheit": "75",
"celsius": "24"
},
"low": {
"fahrenheit": "59",
"celsius": "15"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "sunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 10,
"kph": 16,
"dir": "West",
"degrees": 264
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "West",
"degrees": 263
},
"avehumidity": 78,
"maxhumidity": 90,
"minhumidity": 58
}, {
"date": {
"epoch": "1342072800",
"pretty": "11:00 PM PDT on July 11, 2012",
"day": 11,
"month": 7,
"year": 2012,
"yday": 192,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Wed",
"weekday": "Wednesday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 9,
"high": {
"fahrenheit": "73",
"celsius": "23"
},
"low": {
"fahrenheit": "59",
"celsius": "15"
},
"conditions": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"skyicon": "mostlysunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 11,
"kph": 18,
"dir": "West",
"degrees": 273
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "West",
"degrees": 268
},
"avehumidity": 76,
"maxhumidity": 93,
"minhumidity": 58
}, {
"date": {
"epoch": "1342159200",
"pretty": "11:00 PM PDT on July 12, 2012",
"day": 12,
"month": 7,
"year": 2012,
"yday": 193,
"hour": 23,
"min": "00",
"sec": 0,
"isdst": "1",
"monthname": "July",
"weekday_short": "Thu",
"weekday": "Thursday",
"ampm": "PM",
"tz_short": "PDT",
"tz_long": "America/Los_Angeles"
},
"period": 10,
"high": {
"fahrenheit": "73",
"celsius": "23"
},
"low": {
"fahrenheit": "57",
"celsius": "14"
},
"conditions": "Clear",
"icon": "clear",
"icon_url": "http://icons-ak.wxug.com/i/c/k/clear.gif",
"skyicon": "sunny",
"pop": 0,
"qpf_allday": {
"in": 0.00,
"mm": 0.0
},
"qpf_day": {
"in": 0.00,
"mm": 0.0
},
"qpf_night": {
"in": 0.00,
"mm": 0.0
},
"snow_allday": {
"in": 0,
"cm": 0
},
"snow_day": {
"in": 0,
"cm": 0
},
"snow_night": {
"in": 0,
"cm": 0
},
"maxwind": {
"mph": 11,
"kph": 18,
"dir": "West",
"degrees": 268
},
"avewind": {
"mph": 8,
"kph": 13,
"dir": "West",
"degrees": 263
},
"avehumidity": 71,
"maxhumidity": 89,
"minhumidity": 53
}]
}
}
}
我试图深入到:forecast>txt\u forecast>{icon,icon\u url,title,fcttext}
我目前使用的是一个专用的JSONParser类,它不太好用。目前看起来是这样的:
package com.garciaericn.forecaster.data;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Full Sail University
* Mobile Development BS
* Created by ENG618-Mac on 9/7/14.
*/
public class JSONParser {
public static final String TAG = "JSONParser.TAG";
public static List<Weather> parseForecast(String jsonString) {
Log.i(TAG, "parseForecast entered");
List<Weather> weatherJSON = new ArrayList<Weather>();
try {
// Create JSON
JSONObject data = new JSONObject(jsonString);
// Create Array of weather objects
JSONArray forecast = new JSONArray(data.getJSONArray("forecast").toString());
JSONObject txtForecast = forecast.getJSONObject("txt_forecast");
JSONObject simpeForecast = forecast.getJSONObject(Integer.parseInt("simpleforecast"));
// Loop through recipesJSON for desired info
for (int i = 0; i < txtForecast.length(); i++) {
Log.i(TAG, "In for loop at index: " + i + " Object: " + forecast.getJSONObject(i));
// This is where I was tring to obtain those fields
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.i(TAG, "JSON array: " + weatherJSON);
return weatherJSON;
}
}
package com.garciaericn.forecaster.data;
导入android.util.Log;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.List;
/**
*满帆大学
*移动开发BS
*由ENG618 Mac于2014年9月7日创建。
*/
公共类JSONParser{
公共静态最终字符串TAG=“JSONParser.TAG”;
公共静态列表parseForecast(字符串jsonString){
Log.i(标记“parseForecast entered”);
List weatherJSON=new ArrayList();
试一试{
//创建JSON
JSONObject数据=新的JSONObject(jsonString);
//创建天气对象数组
JSONArray forecast=newjsonarray(data.getJSONArray(“forecast”).toString());
JSONObject txtForecast=forecast.getJSONObject(“txt_forecast”);
JSONObject simpeForecast=forecast.getJSONObject(Integer.parseInt(“simpleforecast”);
//循环搜索所需的信息
对于(int i=0;i JSON中的数组以[
开头,而对象以{
开头。forecast
在JSON中不是数组,因此请更改此设置
JSONArray forecast = new JSONArray(data.getJSONArray("forecast").toString());
到
另外,forecastday
是数组。因此相应地更改逻辑。这里是我为自己工作的try/catch
try {
// Create JSON
JSONObject data = new JSONObject(jsonString);
// Create Array of weather objects
JSONObject forecast = new JSONObject(data.getJSONObject("forecast").toString());
JSONObject txtForecast = new JSONObject(forecast.getJSONObject("txt_forecast").toString());
JSONArray forecastDay = txtForecast.getJSONArray("forecastday");
// TODO: Add more details to weather object with following info.
//JSONObject simpleForecast = forecast.getJSONObject(forecast.getJSONObject("simpleforecast").toString());
//JSONArray simpleForecastDay = simpleForecast.getJSONArray("forecastday");
// Loop through recipesJSON for desired info
for (int i = 0; i < forecastDay.length(); i++) {
Log.i(TAG, "In for loop at index: " + i + " Object: " + forecastDay.getJSONObject(i));
// Obtain Fields
String dayOfWeek = forecastDay.getJSONObject(i).getString("title");
String condition = forecastDay.getJSONObject(i).getString("icon");
String forecastText = forecastDay.getJSONObject(i).getString("fcttext");
String iconURL = forecastDay.getJSONObject(i).getString("icon_url");
// Create instance of Weather
Weather tempWeather = new Weather(dayOfWeek, condition, forecastText, iconURL);
weatherJSON.add(tempWeather);
}
} catch (JSONException e) {
e.printStackTrace();
}
试试看{
//创建JSON
JSONObject数据=新的JSONObject(jsonString);
//创建天气对象数组
JSONObject forecast=新的JSONObject(data.getJSONObject(“forecast”).toString();
JSONObject txtForecast=新的JSONObject(forecast.getJSONObject(“txt_forecast”).toString();
JSONArray forecastDay=txtfecast.getJSONArray(“forecastDay”);
//TODO:使用以下信息向天气对象添加更多详细信息。
//JSONObject simpleForecast=forecast.getJSONObject(forecast.getJSONObject(“simpleForecast”).toString());
//JSONArray simpleForecastDay=simpleForecast.getJSONArray(“forecastday”);
//循环搜索所需的信息
对于(int i=0;i
任何可靠的解析器在这方面都应该没有问题。你只需要了解它是什么。去json.org学习语法——学习只需要5-10分钟,然后事情就会变得更有意义。请注意,试图复制别人的json处理代码通常是一件傻事,因为代码必须不同(从根本上说)基于JSON的结构。(你的代码中有几处完全错误——基本上是胡说八道。)(不幸的是,你的JSON太长了,无法进行后期格式化,但你可以通过将其粘贴到at来查看格式化的JSON,缩进会使它更容易理解。)这是胡说八道:forecast.getJSONObject(Integer.parseInt(“simpleforecast”)
。这是一种艰难的做法,没有什么好的理由:newjsonarray(data.getJSONArray(“forecast”).toString())
谢谢,这确实帮助我理解了我的错误所在。
try {
// Create JSON
JSONObject data = new JSONObject(jsonString);
// Create Array of weather objects
JSONObject forecast = new JSONObject(data.getJSONObject("forecast").toString());
JSONObject txtForecast = new JSONObject(forecast.getJSONObject("txt_forecast").toString());
JSONArray forecastDay = txtForecast.getJSONArray("forecastday");
// TODO: Add more details to weather object with following info.
//JSONObject simpleForecast = forecast.getJSONObject(forecast.getJSONObject("simpleforecast").toString());
//JSONArray simpleForecastDay = simpleForecast.getJSONArray("forecastday");
// Loop through recipesJSON for desired info
for (int i = 0; i < forecastDay.length(); i++) {
Log.i(TAG, "In for loop at index: " + i + " Object: " + forecastDay.getJSONObject(i));
// Obtain Fields
String dayOfWeek = forecastDay.getJSONObject(i).getString("title");
String condition = forecastDay.getJSONObject(i).getString("icon");
String forecastText = forecastDay.getJSONObject(i).getString("fcttext");
String iconURL = forecastDay.getJSONObject(i).getString("icon_url");
// Create instance of Weather
Weather tempWeather = new Weather(dayOfWeek, condition, forecastText, iconURL);
weatherJSON.add(tempWeather);
}
} catch (JSONException e) {
e.printStackTrace();
}