收到响应后Json解析错误
本文关键字:错误 Json 响应 | 更新日期: 2023-09-27 18:15:58
我正在尝试解析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'来自命名空间" expected "。发现元素'DID' from namespace " .
我以前没有使用过json,所以我认为它是开始一个新的部分?如果有,我该如何处理?
您的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; }
}
}
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; }
}
如果只在一个地方立即使用数据,也可以定义匿名类。
var definition = new
{
credit = "",
credit_URL = "",
disclaimer_url = "",
copyright_url = "",
privacy_policy_url = "",
image = new
{
url = "",
title = "",
link = ""
}, ...
}
并像这样使用
var resultObject = JsonConvert.DeserializeAnonymousType(jsonString, definition);