我试图解析一个json响应,但我没有取得任何成功。
这是我的代码:
public static DavisJsonData GetReadings()
{
var username = "test";
var password = "test";
var token = "001d0a00101010101010101";
var url = string.Format("http://someapi.com/my.json?user={0}&pass={1}&apiToken={2}", username, password, token);
var response = WebRequestClasses.GetResponse(url);
DataContractJsonSerializer jsonObjectPersonInfo = new DataContractJsonSerializer(typeof(DavisJsonData));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(response));
DavisJsonData result = (DavisJsonData)jsonObjectPersonInfo.ReadObject(stream);
return result;
}
我的DavisJsonData课程:
namespace WeatherWizard.Davis
{
public class DavisJsonData
{
public string dewpoint_c { set; get; }
public string dewpoint_f { set; get; }
public string dewpoint_string { set; get; }
public string heat_index_c { set; get; }
public string heat_index_f { set; get; }
public string heat_index_string { set; get; }
public string location { set; get; }
public string latitude { set; get; }
public string longitude { set; get; }
public string observation_time { set; get; }
public string observation_time_rfc822 { set; get; }
public string pressure_in { set; get; }
public string pressure_mb { set; get; }
public string pressure_string { set; get; }
public string relative_humidity { set; get; }
public string station_id { set; get; }
public string temp_c { set; get; }
public string temp_f { set; get; }
public string temperature_string { set; get; }
public string wind_degrees { set; get; }
public string wind_dir { set; get; }
public string wind_kt { set; get; }
public string wind_mph { set; get; }
public string windchill_c { set; get; }
public string windchill_f { set; get; }
public string windchill_string { set; get; }
public string davis_current_observation { set; get; }
public string station_name { set; get; }
public string observation_age { set; get; }
public string dewpoint_day_high_time { set; get; }
public string dewpoint_day_low_f { set; get; }
public string dewpoint_day_low_time { set; get; }
public string dewpoint_month_high_f { set; get; }
public string dewpoint_month_low_f { set; get; }
public string dewpoint_year_high_f { set; get; }
public string dewpoint_year_low_f { set; get; }
public string et_day { set; get; }
public string et_month { set; get; }
public string et_year { set; get; }
public string heat_index_day_high_f { set; get; }
public string heat_index_day_high_time { set; get; }
public string heat_index_month_high_f { set; get; }
public string heat_index_year_high_f { set; get; }
public string pressure_day_high_in { set; get; }
public string pressure_day_high_time { set; get; }
public string pressure_day_low_in { set; get; }
public string pressure_day_low_time { set; get; }
public string pressure_month_high_in { set; get; }
public string pressure_month_low_in { set; get; }
public string pressure_tendency_string { set; get; }
public string pressure_year_high_in { set; get; }
public string pressure_year_low_in { set; get; }
public string rain_day_in { set; get; }
public string rain_month_in { set; get; }
public string rain_rate_day_high_in_per_hr { set; get; }
public string rain_rate_hour_high_in_per_hr { set; get; }
public string rain_rate_in_per_hr { set; get; }
public string rain_rate_month_high_in_per_hr { set; get; }
public string rain_rate_year_high_in_per_hr { set; get; }
public string rain_storm_in { set; get; }
public string rain_year_in { set; get; }
public string relative_humidity_day_high { set; get; }
public string relative_humidity_day_high_time { set; get; }
public string relative_humidity_day_low { set; get; }
public string relative_humidity_day_low_time { set; get; }
public string relative_humidity_month_high { set; get; }
public string relative_humidity_month_low { set; get; }
public string relative_humidity_year_high { set; get; }
public string relative_humidity_year_low { set; get; }
public string relative_humidity_in { set; get; }
public string relative_humidity_in_day_high { set; get; }
public string relative_humidity_in_day_high_time { set; get; }
public string relative_humidity_in_day_low { set; get; }
public string relative_humidity_in_day_low_time { set; get; }
public string relative_humidity_in_month_high { set; get; }
public string relative_humidity_in_month_low { set; get; }
public string relative_humidity_in_year_high { set; get; }
public string relative_humidity_in_year_low { set; get; }
public string relative_humidity_1 { set; get; }
public string relative_humidity_1_day_high { set; get; }
public string relative_humidity_1_day_high_time { set; get; }
public string relative_humidity_1_day_low { set; get; }
public string relative_humidity_1_day_low_time { set; get; }
public string relative_humidity_1_month_high { set; get; }
public string relative_humidity_1_month_low { set; get; }
public string relative_humidity_1_year_high { set; get; }
public string relative_humidity_1_year_low { set; get; }
public string solar_radiation { set; get; }
public string solar_radiation_day_high { set; get; }
public string solar_radiation_day_high_time { set; get; }
public string solar_radiation_month_high { set; get; }
public string solar_radiation_year_high { set; get; }
public string sunrise { set; get; }
public string sunset { set; get; }
public string temp_day_high_f { set; get; }
public string temp_day_high_time { set; get; }
public string temp_day_low_f { set; get; }
public string temp_day_low_time { set; get; }
public string temp_month_high_f { set; get; }
public string temp_month_low_f { set; get; }
public string temp_year_high_f { set; get; }
public string temp_year_low_f { set; get; }
public string temp_extra_1 { set; get; }
public string temp_extra_1_day_high { set; get; }
public string temp_extra_1_day_high_time { set; get; }
public string temp_extra_1_day_low { set; get; }
public string temp_extra_1_day_low_time { set; get; }
public string temp_extra_1_month_high { set; get; }
public string temp_extra_1_month_low { set; get; }
public string temp_extra_1_year_high { set; get; }
public string temp_extra_1_year_low { set; get; }
public string temp_in_day_high_f { set; get; }
public string temp_in_day_high_time { set; get; }
public string temp_in_day_low_f { set; get; }
public string temp_in_day_low_time { set; get; }
public string temp_in_f { set; get; }
public string temp_in_month_high_f { set; get; }
public string temp_in_month_low_f { set; get; }
public string temp_in_year_high_f { set; get; }
public string temp_in_year_low_f { set; get; }
public string uv_index { set; get; }
public string uv_index_day_high { set; get; }
public string uv_index_day_high_time { set; get; }
public string uv_index_month_high { set; get; }
public string uv_index_year_high { set; get; }
public string wind_day_high_mph { set; get; }
public string wind_day_high_time { set; get; }
public string wind_month_high_mph { set; get; }
public string wind_ten_min_avg_mph { set; get; }
public string wind_ten_min_gust_mph { set; get; }
public string wind_year_high_mph { set; get; }
public string windchill_day_low_f { set; get; }
public string windchill_day_low_time { set; get; }
public string windchill_month_low_f { set; get; }
public string windchill_year_low_f { set; get; }
}
}
这是我想要解析的回复示例:
{"credit":"Davis Instruments Corp.","credit_URL":"http:\/\/www.davisnet.com","disclaimer_url":"http:\/\/www.davisnet.com\/about\/terms.asp","copyright_url":"http:\/\/www.davisnet.com\/about\/terms.asp","privacy_policy_url":"http:\/\/www.davisnet.com\/about\/privacy.asp","image":{"url":"http:\/\/www.weatherlink.com\/images\/Logo_Davis_reflxblu.jpg","title":"Davis WeatherLink","link":"http:\/\/www.weatherlink.com"},"suggested_pickup":"15 minutes after the hour","suggested_pickup_period":"60","dewpoint_c":"15.6","dewpoint_f":"60.0","dewpoint_string":"60.0 F (15.6 C)","heat_index_c":"21.7","heat_index_f":"71.0","heat_index_string":"71.0 F (21.7 C)","location":"Hayward, CA, USA","latitude":"37.709899354855","longitude":"-121.55822753906","observation_time":"Last Updated on Feb 5 2015, 7:59 pm ART","observation_time_rfc822":"Thu, 5 Feb 2015 19:59:13 -0300","pressure_in":"29.891","pressure_mb":"1012.2","pressure_string":"1012.2 mb","relative_humidity":"69","station_id":"demo","temp_c":"21.7","temp_f":"71.1","temperature_string":"71.1 F (21.7 C)","wind_degrees":"286","wind_dir":"West","wind_kt":"7.0","wind_mph":"8.0","windchill_c":"21.1","windchill_f":"70.0","windchill_string":"70.0 F (21.1 C)","davis_current_observation":{"DID":"001D0A002BC5","station_name":"Davis Instruments - Hayward, CA","observation_age":54939849,"dewpoint_day_high_f":"64","dewpoint_day_high_time":"12:00am","dewpoint_day_low_f":"58","dewpoint_day_low_time":"10:01pm","dewpoint_month_high_f":"65","dewpoint_month_low_f":"34","dewpoint_year_high_f":"75","dewpoint_year_low_f":"11","et_day":"0.050","et_month":"0.880","et_year":"16.470","heat_index_day_high_f":"75","heat_index_day_high_time":"9:42pm","heat_index_month_high_f":"81","heat_index_year_high_f":"102","pressure_day_high_in":"30.012","pressure_day_high_time":"7:54am","pressure_day_low_in":"29.888","pressure_day_low_time":"10:55pm","pressure_month_high_in":"30.227","pressure_month_low_in":"29.853","pressure_tendency_string":"Falling Slowly","pressure_year_high_in":"30.415","pressure_year_low_in":"29.475","rain_day_in":"0.0000","rain_month_in":"0.0000","rain_rate_day_high_in_per_hr":"0.0000","rain_rate_hour_high_in_per_hr":"0.0000","rain_rate_in_per_hr":"0.0000","rain_rate_month_high_in_per_hr":"0.0000","rain_rate_year_high_in_per_hr":"3.7400","rain_storm_in":"0.0000","rain_year_in":"11.4000","relative_humidity_day_high":"92","relative_humidity_day_high_time":"7:33am","relative_humidity_day_low":"57","relative_humidity_day_low_time":"10:07pm","relative_humidity_month_high":"96","relative_humidity_month_low":"18","relative_humidity_year_high":"100","relative_humidity_year_low":"11","relative_humidity_in":"37","relative_humidity_in_day_high":"44","relative_humidity_in_day_high_time":"12:00am","relative_humidity_in_day_low":"37","relative_humidity_in_day_low_time":"11:10pm","relative_humidity_in_month_high":"44","relative_humidity_in_month_low":"29","relative_humidity_in_year_high":"54","relative_humidity_in_year_low":"18","relative_humidity_1":"71","relative_humidity_1_day_high":"92","relative_humidity_1_day_high_time":"9:10am","relative_humidity_1_day_low":"61","relative_humidity_1_day_low_time":"10:07pm","relative_humidity_1_month_high":"94","relative_humidity_1_month_low":"19","relative_humidity_1_year_high":"98","relative_humidity_1_year_low":"12","solar_radiation":"158","solar_radiation_day_high":"696","solar_radiation_day_high_time":"9:57pm","solar_radiation_month_high":"722","solar_radiation_year_high":"1139","sunrise":"5:47am","sunset":"8:30pm","temp_day_high_f":"74.0","temp_day_high_time":"10:00pm","temp_day_low_f":"62.0","temp_day_low_time":"9:09am","temp_month_high_f":"83.8","temp_month_low_f":"55.1","temp_year_high_f":"100.7","temp_year_low_f":"46.0","temp_extra_1":"61.0","temp_extra_1_day_high":"63.0","temp_extra_1_day_high_time":"9:33pm","temp_extra_1_day_low":"51.0","temp_extra_1_day_low_time":"9:08am","temp_extra_1_month_high":"74.0","temp_extra_1_month_low":"44.0","temp_extra_1_year_high":"90.0","temp_extra_1_year_low":"35.0","temp_in_day_high_f":"75.2","temp_in_day_high_time":"12:04am","temp_in_day_low_f":"70.6","temp_in_day_low_time":"12:33pm","temp_in_f":"74.6","temp_in_month_high_f":"79.6","temp_in_month_low_f":"66.6","temp_in_year_high_f":"83.6","temp_in_year_low_f":"62.7","uv_index":"1.0","uv_index_day_high":"3.3","uv_index_day_high_time":"8:56pm","uv_index_month_high":"3.3","uv_index_year_high":"9.6","wind_day_high_mph":"13.0","wind_day_high_time":"12:04am","wind_month_high_mph":"17.0","wind_ten_min_avg_mph":"7.0","wind_ten_min_gust_mph":"11.0","wind_year_high_mph":"34.0","windchill_day_low_f":"62","windchill_day_low_time":"8:22am","windchill_month_low_f":"55","windchill_year_low_f":"45"},"time_to_generate":"0.058525 seconds"}
我尝试运行时遇到此错误:
未处理的类型异常 发生'System.Runtime.Serialization.SerializationException' System.Runtime.Serialization.dll
其他信息:反序列化对象时出错 键入WeatherWizard.Davis.DavisJsonData。结束元素 'davis_current_observation'来自命名空间''预期。找到元素 'DID'来自命名空间''。
之前我没有和json合作过,所以我认为它会开始一个新的部分?如果是这样,我该如何处理呢?
答案 0 :(得分:2)
API将“davis_current_observation”作为单独的对象返回。所以添加一个新类并在主类“DavisJsonData”中添加一个属性,类型为“Davis_Current_Observation”
Public Class DavisJsonData
{
Public Davis_Current_Observation davis_current_observation { set; get; }
}
Public class Davis_Current_Observation
{
public string davis_current_observation { set; get; }
public string station_name { set; get; }
public string observation_age { set; get; }
public string dewpoint_day_high_time { set; get; }
public string dewpoint_day_low_f { set; get; }
public string dewpoint_day_low_time { set; get; }
public string dewpoint_month_high_f { set; get; }
public string dewpoint_month_low_f { set; get; }
public string dewpoint_year_high_f { set; get; }
public string dewpoint_year_low_f { set; get; }
public string et_day { set; get; }
public string et_month { set; get; }
public string et_year { set; get; }
public string heat_index_day_high_f { set; get; }
public string heat_index_day_high_time { set; get; }
public string heat_index_month_high_f { set; get; }
public string heat_index_year_high_f { set; get; }
public string pressure_day_high_in { set; get; }
public string pressure_day_high_time { set; get; }
public string pressure_day_low_in { set; get; }
public string pressure_day_low_time { set; get; }
public string pressure_month_high_in { set; get; }
public string pressure_month_low_in { set; get; }
public string pressure_tendency_string { set; get; }
public string pressure_year_high_in { set; get; }
public string pressure_year_low_in { set; get; }
public string rain_day_in { set; get; }
public string rain_month_in { set; get; }
public string rain_rate_day_high_in_per_hr { set; get; }
public string rain_rate_hour_high_in_per_hr { set; get; }
public string rain_rate_in_per_hr { set; get; }
public string rain_rate_month_high_in_per_hr { set; get; }
public string rain_rate_year_high_in_per_hr { set; get; }
public string rain_storm_in { set; get; }
public string rain_year_in { set; get; }
public string relative_humidity_day_high { set; get; }
public string relative_humidity_day_high_time { set; get; }
public string relative_humidity_day_low { set; get; }
public string relative_humidity_day_low_time { set; get; }
public string relative_humidity_month_high { set; get; }
public string relative_humidity_month_low { set; get; }
public string relative_humidity_year_high { set; get; }
public string relative_humidity_year_low { set; get; }
public string relative_humidity_in { set; get; }
public string relative_humidity_in_day_high { set; get; }
public string relative_humidity_in_day_high_time { set; get; }
public string relative_humidity_in_day_low { set; get; }
public string relative_humidity_in_day_low_time { set; get; }
public string relative_humidity_in_month_high { set; get; }
public string relative_humidity_in_month_low { set; get; }
public string relative_humidity_in_year_high { set; get; }
public string relative_humidity_in_year_low { set; get; }
public string relative_humidity_1 { set; get; }
public string relative_humidity_1_day_high { set; get; }
public string relative_humidity_1_day_high_time { set; get; }
public string relative_humidity_1_day_low { set; get; }
public string relative_humidity_1_day_low_time { set; get; }
public string relative_humidity_1_month_high { set; get; }
public string relative_humidity_1_month_low { set; get; }
public string relative_humidity_1_year_high { set; get; }
public string relative_humidity_1_year_low { set; get; }
public string solar_radiation { set; get; }
public string solar_radiation_day_high { set; get; }
public string solar_radiation_day_high_time { set; get; }
public string solar_radiation_month_high { set; get; }
public string solar_radiation_year_high { set; get; }
public string sunrise { set; get; }
public string sunset { set; get; }
public string temp_day_high_f { set; get; }
public string temp_day_high_time { set; get; }
public string temp_day_low_f { set; get; }
public string temp_day_low_time { set; get; }
public string temp_month_high_f { set; get; }
public string temp_month_low_f { set; get; }
public string temp_year_high_f { set; get; }
public string temp_year_low_f { set; get; }
public string temp_extra_1 { set; get; }
public string temp_extra_1_day_high { set; get; }
public string temp_extra_1_day_high_time { set; get; }
public string temp_extra_1_day_low { set; get; }
public string temp_extra_1_day_low_time { set; get; }
public string temp_extra_1_month_high { set; get; }
public string temp_extra_1_month_low { set; get; }
public string temp_extra_1_year_high { set; get; }
public string temp_extra_1_year_low { set; get; }
public string temp_in_day_high_f { set; get; }
public string temp_in_day_high_time { set; get; }
public string temp_in_day_low_f { set; get; }
public string temp_in_day_low_time { set; get; }
public string temp_in_f { set; get; }
public string temp_in_month_high_f { set; get; }
public string temp_in_month_low_f { set; get; }
public string temp_in_year_high_f { set; get; }
public string temp_in_year_low_f { set; get; }
public string uv_index { set; get; }
public string uv_index_day_high { set; get; }
public string uv_index_day_high_time { set; get; }
public string uv_index_month_high { set; get; }
public string uv_index_year_high { set; get; }
public string wind_day_high_mph { set; get; }
public string wind_day_high_time { set; get; }
public string wind_month_high_mph { set; get; }
public string wind_ten_min_avg_mph { set; get; }
public string wind_ten_min_gust_mph { set; get; }
public string wind_year_high_mph { set; get; }
public string windchill_day_low_f { set; get; }
public string windchill_day_low_time { set; get; }
public string windchill_month_low_f { set; get; }
public string windchill_year_low_f { set; get; }
}
答案 1 :(得分:2)
您的DavisJsonData
类无法正确反映您要解析的json。您的davis_current_observation
属性是一个对象而不是字符串。请试试这个:
public class DavisJsonData
{
public string dewpoint_c { set; get; }
public string dewpoint_f { set; get; }
public string dewpoint_string { set; get; }
public string heat_index_c { set; get; }
public string heat_index_f { set; get; }
public string heat_index_string { set; get; }
public string location { set; get; }
public string latitude { set; get; }
public string longitude { set; get; }
public string observation_time { set; get; }
public string observation_time_rfc822 { set; get; }
public string pressure_in { set; get; }
public string pressure_mb { set; get; }
public string pressure_string { set; get; }
public string relative_humidity { set; get; }
public string station_id { set; get; }
public string temp_c { set; get; }
public string temp_f { set; get; }
public string temperature_string { set; get; }
public string wind_degrees { set; get; }
public string wind_dir { set; get; }
public string wind_kt { set; get; }
public string wind_mph { set; get; }
public string windchill_c { set; get; }
public string windchill_f { set; get; }
public string windchill_string { set; get; }
public Observation davis_current_observation { set; get; }
public class Observation
{
public string station_name { set; get; }
public string observation_age { set; get; }
public string dewpoint_day_high_time { set; get; }
public string dewpoint_day_low_f { set; get; }
public string dewpoint_day_low_time { set; get; }
public string dewpoint_month_high_f { set; get; }
public string dewpoint_month_low_f { set; get; }
public string dewpoint_year_high_f { set; get; }
public string dewpoint_year_low_f { set; get; }
public string et_day { set; get; }
public string et_month { set; get; }
public string et_year { set; get; }
public string heat_index_day_high_f { set; get; }
public string heat_index_day_high_time { set; get; }
public string heat_index_month_high_f { set; get; }
public string heat_index_year_high_f { set; get; }
public string pressure_day_high_in { set; get; }
public string pressure_day_high_time { set; get; }
public string pressure_day_low_in { set; get; }
public string pressure_day_low_time { set; get; }
public string pressure_month_high_in { set; get; }
public string pressure_month_low_in { set; get; }
public string pressure_tendency_string { set; get; }
public string pressure_year_high_in { set; get; }
public string pressure_year_low_in { set; get; }
public string rain_day_in { set; get; }
public string rain_month_in { set; get; }
public string rain_rate_day_high_in_per_hr { set; get; }
public string rain_rate_hour_high_in_per_hr { set; get; }
public string rain_rate_in_per_hr { set; get; }
public string rain_rate_month_high_in_per_hr { set; get; }
public string rain_rate_year_high_in_per_hr { set; get; }
public string rain_storm_in { set; get; }
public string rain_year_in { set; get; }
public string relative_humidity_day_high { set; get; }
public string relative_humidity_day_high_time { set; get; }
public string relative_humidity_day_low { set; get; }
public string relative_humidity_day_low_time { set; get; }
public string relative_humidity_month_high { set; get; }
public string relative_humidity_month_low { set; get; }
public string relative_humidity_year_high { set; get; }
public string relative_humidity_year_low { set; get; }
public string relative_humidity_in { set; get; }
public string relative_humidity_in_day_high { set; get; }
public string relative_humidity_in_day_high_time { set; get; }
public string relative_humidity_in_day_low { set; get; }
public string relative_humidity_in_day_low_time { set; get; }
public string relative_humidity_in_month_high { set; get; }
public string relative_humidity_in_month_low { set; get; }
public string relative_humidity_in_year_high { set; get; }
public string relative_humidity_in_year_low { set; get; }
public string relative_humidity_1 { set; get; }
public string relative_humidity_1_day_high { set; get; }
public string relative_humidity_1_day_high_time { set; get; }
public string relative_humidity_1_day_low { set; get; }
public string relative_humidity_1_day_low_time { set; get; }
public string relative_humidity_1_month_high { set; get; }
public string relative_humidity_1_month_low { set; get; }
public string relative_humidity_1_year_high { set; get; }
public string relative_humidity_1_year_low { set; get; }
public string solar_radiation { set; get; }
public string solar_radiation_day_high { set; get; }
public string solar_radiation_day_high_time { set; get; }
public string solar_radiation_month_high { set; get; }
public string solar_radiation_year_high { set; get; }
public string sunrise { set; get; }
public string sunset { set; get; }
public string temp_day_high_f { set; get; }
public string temp_day_high_time { set; get; }
public string temp_day_low_f { set; get; }
public string temp_day_low_time { set; get; }
public string temp_month_high_f { set; get; }
public string temp_month_low_f { set; get; }
public string temp_year_high_f { set; get; }
public string temp_year_low_f { set; get; }
public string temp_extra_1 { set; get; }
public string temp_extra_1_day_high { set; get; }
public string temp_extra_1_day_high_time { set; get; }
public string temp_extra_1_day_low { set; get; }
public string temp_extra_1_day_low_time { set; get; }
public string temp_extra_1_month_high { set; get; }
public string temp_extra_1_month_low { set; get; }
public string temp_extra_1_year_high { set; get; }
public string temp_extra_1_year_low { set; get; }
public string temp_in_day_high_f { set; get; }
public string temp_in_day_high_time { set; get; }
public string temp_in_day_low_f { set; get; }
public string temp_in_day_low_time { set; get; }
public string temp_in_f { set; get; }
public string temp_in_month_high_f { set; get; }
public string temp_in_month_low_f { set; get; }
public string temp_in_year_high_f { set; get; }
public string temp_in_year_low_f { set; get; }
public string uv_index { set; get; }
public string uv_index_day_high { set; get; }
public string uv_index_day_high_time { set; get; }
public string uv_index_month_high { set; get; }
public string uv_index_year_high { set; get; }
public string wind_day_high_mph { set; get; }
public string wind_day_high_time { set; get; }
public string wind_month_high_mph { set; get; }
public string wind_ten_min_avg_mph { set; get; }
public string wind_ten_min_gust_mph { set; get; }
public string wind_year_high_mph { set; get; }
public string windchill_day_low_f { set; get; }
public string windchill_day_low_time { set; get; }
public string windchill_month_low_f { set; get; }
public string windchill_year_low_f { set; get; }
}
}
答案 2 :(得分:0)
如果您只在一个地方立即使用数据,也可以定义匿名类。
var definition = new
{
credit = "",
credit_URL = "",
disclaimer_url = "",
copyright_url = "",
privacy_policy_url = "",
image = new
{
url = "",
title = "",
link = ""
}, ...
}
并像这样使用它
var resultObject = JsonConvert.DeserializeAnonymousType(jsonString, definition);