【问题标题】:Json Deserialize ExceptionJson反序列化异常
【发布时间】:2014-06-06 06:51:49
【问题描述】:

我正在尝试运行以下代码,但出现异常:

“Newtonsoft.Json.JsonSerializationException”类型的异常 发生在 Newtonsoft.Json.DLL 中但未在用户代码中处理

附加信息:将值“国家”转换为类型时出错 'LocalGasPrices.Station'。路径 '',第 1 行,第 9 位。

        var jsonObj = JObject.Parse(URL);
        var results = jsonObj["stations"].Children().Values();
        var details = new List<Station>();

        foreach (JToken result in results)
        {
            var st = result.ToString();
            var searchResult = JsonConvert.DeserializeObject<Station>(st);
            details.Add(searchResult);

        }

        foreach (Station s in details)
        {
            this.txtDebug.Text += s.address;

        }

以下是 JsonDeserialization 的类:

public class Status
{
    public string error { get; set; }
    public int code { get; set; }
    public string description { get; set; }
    public string message { get; set; }
}

public class GeoLocation
{
    public string city_id { get; set; }
    public string city_long { get; set; }
    public string region_short { get; set; }
    public string region_long { get; set; }
    public string country_long { get; set; }
    public string country_id { get; set; }
    public string region_id { get; set; }
}

public class Station
{
    public string country { get; set; }
    public string price { get; set; }
    public string address { get; set; }
    public string diesel { get; set; }
    public string id { get; set; }
    public string lat { get; set; }
    public string lng { get; set; }
    public string station { get; set; }
    public string region { get; set; }
    public string city { get; set; }
    public string date { get; set; }
    public string distance { get; set; }
}

public class RootObject
{
    public Status status { get; set; }
    public GeoLocation geoLocation { get; set; }
    public List<Station> stations { get; set; }
}

以下是 JSON 响应的示例:

 {
    "status": {
        "error": "NO",
        "code": 200,
        "description": "none",
        "message": "Request ok"
    },
    "geoLocation": {
        "city_id": "147",
        "city_long": "Saint-Laurent",
        "region_short": "QC",
        "region_long": "Quebec",
        "country_long": "Canada",
        "country_id": "43",
        "region_id": "35"
    },
    "stations": [
        {
            "country": "Canada",
            "price": "3.65",
            "address": "3885, Boulevard Saint-Rose",
            "diesel": "0",
            "id": "33862",
            "lat": "45.492367",
            "lng": "-73.710915",
            "station": "Shell",
            "region": "Quebec",
            "city": "Saint-Laurent",
            "date": "3 hours agp",
            "distance": "1.9km"
        }
    ]
}

【问题讨论】:

    标签: c# .net json windows-phone-8 json.net


    【解决方案1】:

    我相信你在结果中不需要 .Values() , .Values() 选择了站的属性。

    var results = jsonObj["stations"].Children();
    

    【讨论】:

      【解决方案2】:

      使用以下内容:

      var details = jsonObj["stations"].Select(t => t.ToObject<Station>()).ToList();
      

      【讨论】:

        【解决方案3】:

        将您的班级名称更改为"stations",您可以在 JSON 中获得该名称。

        【讨论】:

          【解决方案4】:

          试试这样的:

          public class JsonData
          {
            [JsonProperty("status")]
            public JObject Status{get;set;}
          
            [JsonProperty("geoLocation")]
            public JObject Location{get;set;}
          
          
            [JsonProperty("stations")]
            public List<JObject> Stations{get;set;}
          }
          
          public class FinalData
          {
            public Status StatusField{get;set;}
            public GeoLocation LocationField{get;set;}
            public List<Station> StationsList{get;set;}
          }
          

          }

          然后

          JsonSerializer serializer = new JsonSerializer();
          JsonReader reader = new JsonTextReader(new StringReader(your_json));
          var jObj = serializer.Deserialize<JsonData>(reader);
          var result = new FinalData();
          result.StatusField = new Status{ message = jObj.Status["message"].ToString();};
          result.LocationField = new GeoLocation{jObj.location["city_id"].ToString();};
          result.StationsList = new List<Station>();
          foreach(var row = jObj.Stations)
          {
            result.StationsList.Add(new Station{country = row["country"].ToString(), address = row["address"].ToString()};
          }
          

          【讨论】:

            【解决方案5】:

            试试下面的。

            首先创建一个RootObject

            public class RootObject
            {
            public RootObject(){}
            public Status status {get;set;}
            public GeoLocation GeoLocation {get;set;}
            public List<Stations> Stations {get;set;}
            }
            
            public class Status
            {
            public Status(){}
            
            [JsonProperty("error")]
            public string Error {get;set;}
            //all properties
            }
            
            public class GeoLocation
            {
            public GeoLocation{get;set;}
            
            [JsonProperty("city_id")]
            public int CityId {get;set;}
            //all properties
            }
            
            public class Station
            {
            public Station(){}
            // all properties just like previous classes
            }
            

            然后:

            var result = serializer.DeserializeObject<RootObject>(jsonInput);
            

            这应该为您提供具有状态、地理位置和站点列表的 RootObject。

            【讨论】:

              猜你喜欢
              • 2023-01-09
              • 1970-01-01
              • 2016-10-08
              • 1970-01-01
              • 2019-08-22
              • 2023-03-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多