c# - 无法将 Json 解析为 .NET 类

标签 c# .net json.net

给定:

类(class):

public class Venue
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("location")]
    public Location Location { get; set; }
}

public class Location
{
    public long Lat { get; set; }
    public long Lng { get; set; }
    public int Distance { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
}

public class Response
{
    private List<Venue> _venues;
    [JsonProperty("venues")]
    public List<Venue> Venues
    {
        get { return _venues ?? (_venues = new List<Venue>()); }
        set { _venues = value; }
    }
}

以及响应请求:

   {
    "meta":{
      "code":200
   },
   "response":{
      "venues":[
         {
            "id":"4f96a5aee4b01cb74e4dc3c6",
            "name":"Centro",
            "contact":{
            },
            "location":{
               "address":"Centro",
               "lat":-21.256906640441052,
               "lng":-48.31978432813259,
               "distance":185,
               "city":"Jaboticabal",
               "state":"SP",
               "country":"Brazil",
               "cc":"BR"
            },
            "canonicalUrl":"https:\/\/foursquare.com\/v\/centro\/4f96a5aee4b01cb74e4dc3c6",
            "categories":[
               {
                  "id":"4f2a25ac4b909258e854f55f",
                  "name":"Neighborhood",
                  "pluralName":"Neighborhoods",
                  "shortName":"Neighborhood",
                  "icon":{
                     "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/parks_outdoors\/neighborhood_",
                     "suffix":".png"
                  },
                  "primary":true
               }
            ],
            "verified":false,
            "restricted":true,
            "stats":{
               "checkinsCount":1106,
               "usersCount":86,
               "tipCount":0
            },
            "specials":{
               "count":0,
               "items":[
               ]
            },
            "hereNow":{
               "count":0,
               "groups":[
               ]
            },
            "referralId":"v-1376511204"
         },
         {
            "id":"4c38b0b21a38ef3b56b39221",
            "name":"Ice by Nice",
            "contact":{
            },
            "location":{
               "address":"Jaboticabal Shopping",
               "lat":-21.25513775,
               "lng":-48.32320093,
               "distance":253,
               "city":"Jaboticabal",
               "state":"SP",
               "country":"Brazil",
               "cc":"BR"
            },
            "canonicalUrl":"https:\/\/foursquare.com\/v\/ice-by-nice\/4c38b0b21a38ef3b56b39221",
            "categories":[
               {
                  "id":"4bf58dd8d48988d1c9941735",
                  "name":"Ice Cream Shop",
                  "pluralName":"Ice Cream Shops",
                  "shortName":"Ice Cream",
                  "icon":{
                     "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/food\/icecream_",
                     "suffix":".png"
                  },
                  "primary":true
               }
            ],
            "verified":false,
            "restricted":true,
            "stats":{
               "checkinsCount":656,
               "usersCount":309,
               "tipCount":15
            },
            "specials":{
               "count":0,
               "items":[
               ]
            },
            "hereNow":{
               "count":2,
               "groups":[
                  {
                     "type":"others",
                     "name":"Other people here",
                     "count":2,
                     "items":[
                     ]
                  }
               ]
            },
            "referralId":"v-1376511204"
         }
      ]
   }
}

像这样使用 JSON.NET 时:

Response response = JsonConvert.DeserializeObject<Response>(jsonString);

反序列化的响应对象有一个空的 field 列表,我做错了什么?

谢谢

最佳答案

您尝试反序列化的内容与您定义的要反序列化的类有些不匹配。不幸的是,您还需要另一层间接。请注意响应有;

// theres an outer object here which contains response
{
     "meta":{ "code":200 }, 
     "response":{
         // you're trying to deserialize this, but it's not the entire response, it's a property of an anonymous object
     }
}

所以如果我开设一个新类(class);

public class ResponseWrapper
{
      public object meta;
      public Response response;
}

而是这样做;

ResponseWrapper response = JsonConvert.DeserializeObject<ResponseWrapper>(jsonString);

然后就可以了。

请注意,当您使用 json.NET 反序列化时,您必须定义一个与 json 完全匹配的结构。在这种情况下,您将忽略最外面的对象。这有点烦人,会导致很多像我刚刚写的代码,但有时就是这样。

关于c# - 无法将 Json 解析为 .NET 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18242216/

相关文章:

c# - 带 iPad 的 MVC.NET 2

c# - 非静态泛型类中没有扩展方法?

c# - 使用 Json.net 将非数组 JSON 对象解析为数组

c# - HttpClient GetStreamAsync 和 HTTP 状态代码?

c# - 如何将 JSON 数组或 JSON 对象转换为对象列表?

c# - 从 C# 使用 Odbc 调用 Oracle 包函数

c# - 在 Windows Phone 8 中以编程方式(逐个单元格)读取/导入现有 Excel 文件

c# - COM Interop 挂起会卡住整个 COM 系统。如何取消COM调用

c# - 有比 MD5 更好的加密方法吗? (。网)

未提供 C# XML 序列化 int 标记值