ErocM ErocM - 1 month ago 9
JSON Question

Json parsing error after getting Response

I am attempting to parse a json response and I'm not having any success.

This is my code:

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;
}


And my DavisJsonData class:

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; }
}
}


This is an sample of my response that I am trying to parse:

{"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"}


I am getting this error when I attempt to run it:


An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
System.Runtime.Serialization.dll

Additional information: There was an error deserializing the object of
type WeatherWizard.Davis.DavisJsonData. End element
'davis_current_observation' from namespace '' expected. Found element
'DID' from namespace ''.


I've not worked with json before so I would assume it is starting a new section? If so, how do I handle it?

Answer

Your DavisJsonData class does not correctly reflect the json you are parsing. Your davis_current_observation property is an object not a string. Please try this:

  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; }
    }
  }
Comments