*请提供反馈,说明我做错了什么 请不要推荐使用NewtonSoft JSON,因为它不起作用*
[DataContract]
public class WeatherContract
{
[DataMember(Name = "version")]
public string version { get; set; }
[DataMember(Name = "weather")]
public string weather { get; set; }
[DataMember(Name = "temp_f")]
public string temp_f { get; set; }
}
string URI =
"http://api.wunderground.com/api/969c30d9979df192/conditions/q/OR/Roseburg.json";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-
www-form-urlencoded";
string stringResult = wc.DownloadString(URI);
ProgLog.Log(stringResult, PROCNAME, MODNAME);
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(stringResult));
StreamReader sr = new StreamReader(ms);
ms.Position = 0;DataContractJsonSerializer ser = new
DataContractJsonSerializer(typeof(WeatherContract));
WeatherContract weather = (WeatherContract)ser.ReadObject(ms);
版本下方,weather和temp_f为空 ProgLog.Log("版本:" + weather.version +":" +"天气:" + weather.weather +" /& #34; + weather.temp_f,PROCNAME,MODNAME);
以下是Weather Underground的原始JSON
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
}
, "current_observation": {
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
"display_location": {
"full":"Roseburg, OR",
"city":"Roseburg",
"state":"OR",
"state_name":"Oregon",
"country":"US",
"country_iso3166":"US",
"zip":"97470",
"magic":"1",
"wmo":"99999",
"latitude":"43.22000122",
"longitude":"-123.33999634",
"elevation":"146.9"
},
"observation_location": {
"full":"North Bend, Oregon",
"city":"North Bend",
"state":"Oregon",
"country":"US",
"country_iso3166":"US",
"latitude":"43.23",
"longitude":"-123.35",
"elevation":"524 ft"
},
"estimated": {
},
"station_id":"MKRBG",
"observation_time":"Last Updated on May 9, 12:10 PM PDT",
"observation_time_rfc822":"Wed, 09 May 2018 12:10:30 -0700",
"observation_epoch":"1525893030",
"local_time_rfc822":"Wed, 09 May 2018 12:13:46 -0700",
"local_epoch":"1525893226",
"local_tz_short":"PDT",
"local_tz_long":"America/Los_Angeles",
"local_tz_offset":"-0700",
"weather":"Mostly Cloudy",
"temperature_string":"64.0 F (17.8 C)",
"temp_f":64.0,
"temp_c":17.8,
"relative_humidity":"48%",
"wind_string":"From the SW at 3.0 MPH",
"wind_dir":"SW",
"wind_degrees":220,
"wind_mph":3.0,
"wind_gust_mph":0,
"wind_kph":4.8,
"wind_gust_kph":0,
"pressure_mb":"1018",
"pressure_in":"30.06",
"pressure_trend":"-",
"dewpoint_string":"45 F (7 C)",
"dewpoint_f":45,
"dewpoint_c":7,
"heat_index_string":"NA",
"heat_index_f":"NA",
"heat_index_c":"NA",
"windchill_string":"NA",
"windchill_f":"NA",
"windchill_c":"NA",
"feelslike_string":"64.0 F (17.8 C)",
"feelslike_f":"64.0",
"feelslike_c":"17.8",
"visibility_mi":"10.0",
"visibility_km":"16.1",
"solarradiation":"--",
"UV":"4","precip_1hr_string":"-999.00 in ( 0 mm)",
"precip_1hr_in":"-999.00",
"precip_1hr_metric":" 0",
"precip_today_string":"0.03 in (1 mm)",
"precip_today_in":"0.03",
"precip_today_metric":"1",
"icon":"mostlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/mostlycloudy.gif",
"forecast_url":"http://www.wunderground.com/US/OR/Roseburg.html",
"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=MKRBG",
"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=43.230000,-123.349998",
"nowcast":""
}
}