我试图从这里反序列化json:
https://www.wunderground.com/weather/api/d/docs?MR=1我是这样反序列化的:
RootObject oData = JsonConvert.DeserializeObject<RootObject>(jsonResponse.ContentString);
我已经使用http://jsonutils.com/来创建我的c#类。
public class Features
{
public int hourly10day { get; set; }
}
public class Response
{
public string version { get; set; }
public string termsofService { get; set; }
public Features features { get; set; }
}
public class FCTTIME
{
public string hour { get; set; }
public string hour_padded { get; set; }
public string min { get; set; }
public string min_unpadded { get; set; }
public string sec { get; set; }
public string year { get; set; }
public string mon { get; set; }
public string mon_padded { get; set; }
public string mon_abbrev { get; set; }
public string mday { get; set; }
public string mday_padded { get; set; }
public string yday { get; set; }
public string isdst { get; set; }
public string epoch { get; set; }
public string pretty { get; set; }
public string civil { get; set; }
public string month_name { get; set; }
public string month_name_abbrev { get; set; }
public string weekday_name { get; set; }
public string weekday_name_night { get; set; }
public string weekday_name_abbrev { get; set; }
public string weekday_name_unlang { get; set; }
public string weekday_name_night_unlang { get; set; }
public string ampm { get; set; }
public string tz { get; set; }
public string age { get; set; }
public string UTCDATE { get; set; }
}
public class Temp
{
public string english { get; set; }
public string metric { get; set; }
}
public class Dewpoint
{
public string english { get; set; }
public string metric { get; set; }
}
public class Wspd
{
public string english { get; set; }
public string metric { get; set; }
}
public class Wdir
{
public string dir { get; set; }
public string degrees { get; set; }
}
public class Windchill
{
public string english { get; set; }
public string metric { get; set; }
}
public class Heatindex
{
public string english { get; set; }
public string metric { get; set; }
}
public class Feelslike
{
public string english { get; set; }
public string metric { get; set; }
}
public class Qpf
{
public string english { get; set; }
public string metric { get; set; }
}
public class Snow
{
public string english { get; set; }
public string metric { get; set; }
}
public class Mslp
{
public string english { get; set; }
public string metric { get; set; }
}
public class HourlyForecast
{
public FCTTIME FCTTIME { get; set; }
public Temp temp { get; set; }
public Dewpoint dewpoint { get; set; }
public string condition { get; set; }
public string icon { get; set; }
public string icon_url { get; set; }
public string fctcode { get; set; }
public string sky { get; set; }
public Wspd wspd { get; set; }
public Wdir wdir { get; set; }
public string wx { get; set; }
public string uvi { get; set; }
public string humidity { get; set; }
public Windchill windchill { get; set; }
public Heatindex heatindex { get; set; }
public Feelslike feelslike { get; set; }
public Qpf qpf { get; set; }
public Snow snow { get; set; }
public string pop { get; set; }
public Mslp mslp { get; set; }
}
public class RootObject
{
public Response response { get; set; }
public IList<HourlyForecast> hourly_forecast { get; set; }
}
但我仍然得到:
将值3072转换为类型"WeatherUnderground.RootObject"时出错。-在newtonsoft . json . serializable . jsonserializerinternalreader。EnsureType(对象值,类型targetType)
我做错了什么?此外,如果这与。net 3.5有任何关系,我正在使用。
谢谢
public class Features
{
public int conditions { get; set; }
}
public class Response
{
public string version { get; set; }
public string termsofService { get; set; }
public Features features { get; set; }
}
public class Image
{
public string url { get; set; }
public string title { get; set; }
public string link { get; set; }
}
public class DisplayLocation
{
public string full { get; set; }
public string city { get; set; }
public string state { get; set; }
public string state_name { get; set; }
public string country { get; set; }
public string country_iso3166 { get; set; }
public string zip { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string elevation { get; set; }
}
public class ObservationLocation
{
public string full { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public string country_iso3166 { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string elevation { get; set; }
}
public class Estimated
{
}
public class CurrentObservation
{
public Image image { get; set; }
public DisplayLocation display_location { get; set; }
public ObservationLocation observation_location { get; set; }
public Estimated estimated { get; set; }
public string station_id { get; set; }
public string observation_time { get; set; }
public string observation_time_rfc822 { get; set; }
public string observation_epoch { get; set; }
public string local_time_rfc822 { get; set; }
public string local_epoch { get; set; }
public string local_tz_short { get; set; }
public string local_tz_long { get; set; }
public string local_tz_offset { get; set; }
public string weather { get; set; }
public string temperature_string { get; set; }
public double temp_f { get; set; }
public double temp_c { get; set; }
public string relative_humidity { get; set; }
public string wind_string { get; set; }
public string wind_dir { get; set; }
public int wind_degrees { get; set; }
public double wind_mph { get; set; }
public string wind_gust_mph { get; set; }
public double wind_kph { get; set; }
public string wind_gust_kph { get; set; }
public string pressure_mb { get; set; }
public string pressure_in { get; set; }
public string pressure_trend { get; set; }
public string dewpoint_string { get; set; }
public int dewpoint_f { get; set; }
public int dewpoint_c { get; set; }
public string heat_index_string { get; set; }
public string heat_index_f { get; set; }
public string heat_index_c { get; set; }
public string windchill_string { get; set; }
public string windchill_f { get; set; }
public string windchill_c { get; set; }
public string feelslike_string { get; set; }
public string feelslike_f { get; set; }
public string feelslike_c { get; set; }
public string visibility_mi { get; set; }
public string visibility_km { get; set; }
public string solarradiation { get; set; }
public string UV { get; set; }
public string precip_1hr_string { get; set; }
public string precip_1hr_in { get; set; }
public string precip_1hr_metric { get; set; }
public string precip_today_string { get; set; }
public string precip_today_in { get; set; }
public string precip_today_metric { get; set; }
public string icon { get; set; }
public string icon_url { get; set; }
public string forecast_url { get; set; }
public string history_url { get; set; }
public string ob_url { get; set; }
}
public class RootObject
{
public Response response { get; set; }
public CurrentObservation current_observation { get; set; }
}
Json: {
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "San Francisco, CA",
"city": "San Francisco",
"state": "CA",
"state_name": "California",
"country": "US",
"country_iso3166": "US",
"zip": "94101",
"latitude": "37.77500916",
"longitude": "-122.41825867",
"elevation": "47.00000000"
},
"observation_location": {
"full": "SOMA - Near Van Ness, San Francisco, California",
"city": "SOMA - Near Van Ness, San Francisco",
"state": "California",
"country": "US",
"country_iso3166": "US",
"latitude": "37.773285",
"longitude": "-122.417725",
"elevation": "49 ft"
},
"estimated": {},
"station_id": "KCASANFR58",
"observation_time": "Last Updated on June 27, 5:27 PM PDT",
"observation_time_rfc822": "Wed, 27 Jun 2012 17:27:13 -0700",
"observation_epoch": "1340843233",
"local_time_rfc822": "Wed, 27 Jun 2012 17:27:14 -0700",
"local_epoch": "1340843234",
"local_tz_short": "PDT",
"local_tz_long": "America/Los_Angeles",
"local_tz_offset": "-0700",
"weather": "Partly Cloudy",
"temperature_string": "66.3 F (19.1 C)",
"temp_f": 66.3,
"temp_c": 19.1,
"relative_humidity": "65%",
"wind_string": "From the NNW at 22.0 MPH Gusting to 28.0 MPH",
"wind_dir": "NNW",
"wind_degrees": 346,
"wind_mph": 22.0,
"wind_gust_mph": "28.0",
"wind_kph": 35.4,
"wind_gust_kph": "45.1",
"pressure_mb": "1013",
"pressure_in": "29.93",
"pressure_trend": "+",
"dewpoint_string": "54 F (12 C)",
"dewpoint_f": 54,
"dewpoint_c": 12,
"heat_index_string": "NA",
"heat_index_f": "NA",
"heat_index_c": "NA",
"windchill_string": "NA",
"windchill_f": "NA",
"windchill_c": "NA",
"feelslike_string": "66.3 F (19.1 C)",
"feelslike_f": "66.3",
"feelslike_c": "19.1",
"visibility_mi": "10.0",
"visibility_km": "16.1",
"solarradiation": "",
"UV": "5",
"precip_1hr_string": "0.00 in ( 0 mm)",
"precip_1hr_in": "0.00",
"precip_1hr_metric": " 0",
"precip_today_string": "0.00 in (0 mm)",
"precip_today_in": "0.00",
"precip_today_metric": "0",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"forecast_url": "http://www.wunderground.com/US/CA/San_Francisco.html",
"history_url": "http://www.wunderground.com/history/airport/KCASANFR58/2012/6/27/DailyHistory.html",
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.773285,-122.417725"
}
}
你的json和Class结构将是这样的。
这样做会给我适当的结果。
RootObject oData = JsonConvert.DeserializeObject<RootObject>(jsonResponse.ContentString);