2

鉴于:

课程:

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

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

谢谢

4

1 回答 1

4

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

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

因此,如果我开设一个新课程;

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

而是这样做;

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

然后它将起作用。

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

于 2013-08-14T21:06:00.827 回答