我正在查询Wunderground服务10天的预测,我需要解析数据。通过使用类Json2Sharp之类的类创建者然后访问RootObject的方法可行,但不是优选的。我需要的是一个简单的令牌解析,但是,我不明白如何处理复杂的JSON结构。例如,如何从这样的嵌套响应中提取工作日和温度(使用C#):
{
"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
}]
}
}
}
答案 0 :(得分:3)
您的首要任务是选择JSON序列化程序。我的偏好是ServiceStack.Text,但也有JSON.NET和其他各种。您的里程可能会有所不同。
下一个任务是创建一个与输出匹配的C#类。完成后,您可以轻松地进行序列化。
在ServiceStack中,它是一个简单的例子:
fold_lines
答案 1 :(得分:0)
使用this tool我能够很好地了解此响应的结构。答案很简单,第一天预测摄氏温度就是:
string tempCelsius = (string)jsonObject.SelectToken("forecast.simpleforecast.forecastday[0].high.celsius");
使用一个好的工具来可视化JSON结构是关键。