简体   繁体   English

使用NewtonSoft解析JSON时出错

[英]Error parsing JSON with NewtonSoft

Merry Christmas stackies! 圣诞快乐堆叠! I'm trying to parse some JSON from the Wunderground weather API and I'm running into an issue when trying to assign values to variables. 我正在尝试从Wunderground天气API解析一些JSON,并且在尝试为变量赋值时遇到了问题。 Here is a link to the JSON. 是JSON的链接。 Just know that the class structure I'm providing is a conglomerate in order to accommodate multiple JSON return file structures. 只要知道我提供的类结构是一个集合体,以适应多个JSON返回文件结构。 I'll give the class structre, then the call from main, and finally the actual method with the error notated. 我将给出类structre,然后是main的调用,最后是带有错误标记的实际方法。 I'm working in Xamarin, formerlly MonoDevelop. 我在Xamarin,前MonoDevelop工作。 Thanks for anything you can come up with! 感谢您提出的任何建议!

The error reads: Newtonsoft.Json.JsonReaderException has been thrown "Cannot convert String to Integer: 5.0 Path 'current_observation.wind_gust_mph', line 60, position 24. 错误读取:Newtonsoft.Json.JsonReaderException已被抛出“无法将字符串转换为整数:5.0路径'current_observation.wind_gust_mph',第60行,第24位。

public class Wunder
{

    //constructor
    public Wunder ()
    {

    }

    //JSON classes
    public class HistoryResponseContainer
    {
        public ResponseInfo response { get; set; }
        public HistoryInfo history { get; set; }
        public Location location { get; set; }
        public CurrentObservation current_observation { get; set; }
    }

    public class ResponseInfo
    {
        public string version { get; set; }
        public string termsofService { get; set; }
        public Dictionary<string, int> features { get; set; }
    }

    public class HistoryInfo
    {
        public WUDate date { get; set; }
        public WUDate utcdate { get; set; }
        public Observation[] observations { get; set; }
        public Dailysummary[] dailysummary { get; set; }
    }

    public class WUDate
    {
        public string pretty { get; set; }
        public string year { get; set; }
        public string mon { get; set; }
        public string mday { get; set; }
        public string hour { get; set; }
        public string min { get; set; }
        public string tzname { get; set; }

        public DateTime Value
        {
            get
            {
                int year = int.Parse(this.year);
                int month = int.Parse(this.mon);
                int day = int.Parse(this.mday);
                int hour = int.Parse(this.hour);
                int minute = int.Parse(this.min);

                var kind = this.tzname == "UTC"
                           ? DateTimeKind.Utc
                           : DateTimeKind.Unspecified;

                return new DateTime(year, month, day, hour, minute, 0, kind);
            }
        }
    }

    public class Observation
    {
        public WUDate date { get; set; }
        public WUDate utcdate { get; set; }
        public string tempm { get; set; }
        public string tempi { get; set; }
        public string dewptm { get; set; }
        public string dewpti { get; set; }
        public string hum { get; set; }
        public string wspdm { get; set; }
        public string wspdi { get; set; }
        public string wgustm { get; set; }
        public string wgusti { get; set; }
        public string wdird { get; set; }
        public string wdire { get; set; }
        public string vism { get; set; }
        public string visi { get; set; }
        public string pressurem { get; set; }
        public string pressurei { get; set; }
        public string windchillm { get; set; }
        public string windchilli { get; set; }
        public string heatindexm { get; set; }
        public string heatindexi { get; set; }
        public string precipm { get; set; }
        public string precipi { get; set; }
        public string conds { get; set; }
        public string icon { get; set; }
        public string fog { get; set; }
        public string rain { get; set; }
        public string snow { get; set; }
        public string hail { get; set; }
        public string thunder { get; set; }
        public string tornado { get; set; }
        public string metar { get; set; }
    }

    public class Dailysummary
    {
        public WUDate date { get; set; }
        public string fog { get; set; }
        public string rain { get; set; }
        public string snow { get; set; }
        public string snowfallm { get; set; }
        public string snowfalli { get; set; }
        public string monthtodatesnowfallm { get; set; }
        public string monthtodatesnowfalli { get; set; }
        public string since1julsnowfallm { get; set; }
        public string since1julsnowfalli { get; set; }
        public string snowdepthm { get; set; }
        public string snowdepthi { get; set; }
        public string hail { get; set; }
        public string thunder { get; set; }
        public string tornado { get; set; }
        public string meantempm { get; set; }
        public string meantempi { get; set; }
        public string meandewptm { get; set; }
        public string meandewpti { get; set; }
        public string meanpressurem { get; set; }
        public string meanpressurei { get; set; }
        public string meanwindspdm { get; set; }
        public string meanwindspdi { get; set; }
        public string meanwdire { get; set; }
        public string meanwdird { get; set; }
        public string meanvism { get; set; }
        public string meanvisi { get; set; }
        public string humidity { get; set; }
        public string maxtempm { get; set; }
        public string maxtempi { get; set; }
        public string mintempm { get; set; }
        public string mintempi { get; set; }
        public string maxhumidity { get; set; }
        public string minhumidity { get; set; }
        public string maxdewptm { get; set; }
        public string maxdewpti { get; set; }
        public string mindewptm { get; set; }
        public string mindewpti { get; set; }
        public string maxpressurem { get; set; }
        public string maxpressurei { get; set; }
        public string minpressurem { get; set; }
        public string minpressurei { get; set; }
        public string maxwspdm { get; set; }
        public string maxwspdi { get; set; }
        public string minwspdm { get; set; }
        public string minwspdi { get; set; }
        public string maxvism { get; set; }
        public string maxvisi { get; set; }
        public string minvism { get; set; }
        public string minvisi { get; set; }
        public string gdegreedays { get; set; }
        public string heatingdegreedays { get; set; }
        public string coolingdegreedays { get; set; }
        public string precipm { get; set; }
        public string precipi { get; set; }
        public string precipsource { get; set; }
        public string heatingdegreedaysnormal { get; set; }
        public string monthtodateheatingdegreedays { get; set; }
        public string monthtodateheatingdegreedaysnormal { get; set; }
        public string since1sepheatingdegreedays { get; set; }
        public string since1sepheatingdegreedaysnormal { get; set; }
        public string since1julheatingdegreedays { get; set; }
        public string since1julheatingdegreedaysnormal { get; set; }
        public string coolingdegreedaysnormal { get; set; }
        public string monthtodatecoolingdegreedays { get; set; }
        public string monthtodatecoolingdegreedaysnormal { get; set; }
        public string since1sepcoolingdegreedays { get; set; }
        public string since1sepcoolingdegreedaysnormal { get; set; }
        public string since1jancoolingdegreedays { get; set; }
        public string since1jancoolingdegreedaysnormal { get; set; }
    }

    public class Station
    {
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public string icao { get; set; }
        public string lat { get; set; }
        public string lon { get; set; }
    }

    public class Airport
    {
        public List<Station> station { get; set; }
    }

    public class Station2
    {
        public string neighborhood { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string country { get; set; }
        public string id { get; set; }
        public double lat { get; set; }
        public double lon { get; set; }
        public int distance_km { get; set; }
        public int distance_mi { get; set; }
    }

    public class Pws
    {
        public List<Station2> station { get; set; }
    }

    public class NearbyWeatherStations
    {
        public Airport airport { get; set; }
        public Pws pws { get; set; }
    }

    public class Location
    {
        public string type { get; set; }
        public string country { get; set; }
        public string country_iso3166 { get; set; }
        public string country_name { get; set; }
        public string state { get; set; }
        public string city { get; set; }
        public string tz_short { get; set; }
        public string tz_long { get; set; }
        public string lat { get; set; }
        public string lon { get; set; }
        public string zip { get; set; }
        public string magic { get; set; }
        public string wmo { get; set; }
        public string l { get; set; }
        public string requesturl { get; set; }
        public string wuiurl { get; set; }
        public NearbyWeatherStations nearby_weather_stations { get; set; }
    }

    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 int wind_gust_mph { get; set; }
        public int wind_kph { get; set; }
        public int 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 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 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 magic { get; set; }
        public string wmo { get; set; }
        public string latitude { get; set; }
        public string longitude { get; set; }
        public string elevation { get; set; }
    }

    public class Estimated
    {
    }

And here is the call from main. 这是来自main的电话。

class MainClass
{
    public static void Main (string[] args)
    {

        Wunder data = new Wunder ();
        string StationID = "KCOBOULD67";

        string CurrentConditions = data.GetCurrentConditions (StationID);

        Console.WriteLine (CurrentConditions); 

    }

}

Lastly, this is the method being called. 最后,这是被调用的方法。 I took out my Wunderground Key. 我拿出了我的Wunderground Key。 If you want an example of the return JSON, check the link at the top. 如果您想要返回JSON的示例,请检查顶部的链接。

    public String GetCurrentConditions(string StationID){

        String url = @"http://api.wunderground.com/api/" + wundergroundkey + "/conditions/q/pws:" + StationID + ".json";
        Uri uri = new Uri(url);
        WebRequest webRequest = WebRequest.Create(uri);
        WebResponse response = webRequest.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        String responseData = streamReader.ReadToEnd();

        var container = JsonConvert.DeserializeObject<HistoryResponseContainer> (responseData);

        String stationid = container.current_observation.station_id;
        String station_lat = container.current_observation.observation_location.latitude;
        String station_lon = container.current_observation.observation_location.longitude;

        String station_data = stationid + station_lat + station_lon;

        return (station_data);

    } //End GetCurrentConditions

Again, the error reads: Newtonsoft.Json.JsonReaderException has been thrown "Cannot convert String to Integer: 5.0 Path 'current_observation.wind_gust_mph', line 60, position 24. 同样,错误读取:Newtonsoft.Json.JsonReaderException已被抛出“无法将字符串转换为整数:5.0路径'current_observation.wind_gust_mph',第60行,第24位。

You provided us a link to the following JSON: 您向我们提供了以下JSON的链接:

http://api.wunderground.com/api/21fd5aec70326254/conditions/q/CA/San_Francisco.json http://api.wunderground.com/api/21fd5aec70326254/conditions/q/CA/San_Francisco.json

As you see there, the property wind_gust_mph is 0 (without the quotes, so an Integer). 如您所见,属性wind_gust_mph为0(不带引号,因此为整数)。 This JSON also works very well. 这个JSON也很好用。 But in your code you use the following JSON: 但是在您的代码中,您使用以下JSON:

http://api.wunderground.com/api/21fd5aec70326254/conditions/q/pws:KCOBOULD67.json http://api.wunderground.com/api/21fd5aec70326254/conditions/q/pws:KCOBOULD67.json

And there the property wind_gust_mph is "4.0" (with the quotes, so a String). 那里的属性wind_gust_mph是“4.0”(带引号,所以是一个字符串)。 And you cannot parse a string property into an integer. 并且您无法将字符串属性解析为整数。 Also the properties for wind_kph and wind_gust_kph are wrong, this should be either string or double: wind_kphwind_gust_kph的属性也是错误的,这应该是字符串或双wind_gust_kph

public string wind_gust_mph { get; set; }
public double wind_kph { get; set; }
public string wind_gust_kph { get; set; }

Hope this helps and merry x-mas. 希望这有助于和快乐的x-mas。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM