错误:不同类型的问题数组与单个无法反序列化当前的JSON对象

时间:2015-06-17 00:55:09

标签: c# json.net

我正在尝试处理来自Auspost网站的数据。返回不同的json,具体取决于是返回1个位置还是多个。我尝试http://json2csharp.com/或直接粘贴为Visual Studio中的json类。我知道我可以写一个转换器 - 但我认为答案可能要简单得多。

错误:无法反序列化当前的JSON对象 问题:如何编写处理这两种类型的类。

** First Type JSon - 多个地方**

int sunRotation = 7.5f * hours;

** First Type Class **

{
  "localities":{
     "locality":[
        {
           "category":"Post Office Boxes",
           "id":618,
           "location":"NORTH SYDNEY",
           "postcode":2055,
           "state":"NSW"
        },
        {
           "category":"Delivery Area",
           "id":512,
           "latitude":-33.8877655,
           "location":"THE UNIVERSITY OF SYDNEY",
           "longitude":151.1883894,
           "postcode":2006,
           "state":"NSW"
        },
        {
           "category":"Post Office Boxes",
           "id":447,
           "location":"UNSW SYDNEY",
           "postcode":1466,
           "state":"NSW"
        }
     ]
  }
}

**第二种类型 - 单一地点**

public class Locality
{
   public string category { get; set; }
   public int id { get; set; }
   public string location { get; set; }
   public int postcode { get; set; }
   public string state { get; set; }
   public double? latitude { get; set; }
   public double? longitude { get; set; }
}

public class Localities
{
    public List<Locality> locality { get; set; }
}

public class RootObject
{
    public Localities localities { get; set; }
}

**第二类** **

{
  "localities":{
     "locality":{
        "category":"Delivery Area",
        "id":728,
        "latitude":-33.732122,
        "location":"COLLAROY BEACH",
        "longitude":151.301232,
        "postcode":2097,
        "state":"NSW"
     }
  }
}

0 个答案:

没有答案