我错过了什么? RestSharp不会反序列化Json

时间:2013-04-18 20:02:43

标签: c# json foursquare restsharp

我正在尝试将json响应从foursquare变回对象。我得到这样的东西

   {
   "meta":{
      "code":200
   },
   "response":{
      "venues":[
         {
            "id":"4abfb58ef964a520be9120e3",
            "name":"Costco",
            "contact":{
               "phone":"6045967435",
               "formattedPhone":"(604) 596-7435"
            },
            "location":{
               "address":"7423 King George Hwy",
               "crossStreet":"btw 76 Avenue & 73A Avenue",
               "lat":49.138259617056015,
               "lng":-122.84723281860352,
               "distance":19000,
               "postalCode":"V3W 5A8",
               "city":"Surrey",
               "state":"BC",
               "country":"Canada",
               "cc":"CA"
            },
            "canonicalUrl":"https:\/\/foursquare.com\/v\/costco\/4abfb58ef964a520be9120e3",
            "categories":[
               {
                  "id":"4bf58dd8d48988d1f6941735",
                  "name":"Department Store",
                  "pluralName":"Department Stores",
                  "shortName":"Department Store",
                  "icon":{
                     "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/shops\/departmentstore_",
                     "suffix":".png"
                  },
                  "primary":true
               }
            ],
            "verified":true,
            "restricted":true,
            "stats":{
               "checkinsCount":2038,
               "usersCount":533,
               "tipCount":12
            },
            "url":"http:\/\/www.costco.ca",
            "specials":{
               "count":0,
               "items":[

               ]
            },
            "hereNow":{
               "count":0,
               "groups":[

               ]
            },
            "referralId":"v-1366316196"
         }
      ]
   }
}

我做了这样的课程

 public class Response
    {
        public string Meta { get; set; }
        public List<Venue> Venues { get; set; }
    }

  public class Venue
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public Contact Contact { get; set; }
        public Location Location { get; set; }
        public string CanonicalUrl { get; set; }
        public Categories Categories { get; set; }
        public bool Verified { get; set; }
    }

 var response = client.Execute<Response>(request);
       var test = response.Data;

然而Venues始终为空。我不知道为什么会这样。

4 个答案:

答案 0 :(得分:19)

好的,我找到了一个很棒的工具http://json2csharp.com/,可以将json转换为对象。我发现通过这个我还需要一个包装来使它工作。

答案 1 :(得分:2)

您只需要更深入地了解JSON响应。 venues属性向上一级的是response属性,该属性目前未在Response类中显示。

你有两种方法可以解决这个问题。

1)添加另一个包装响应对象,其中包含缺少的response属性

// this is the new wrapping object
public class FourSquareResponse
{
    public string Meta { get; set; }
    public VenueResponse Response { get; set; } // previously missing
}

public class VenueResponse
{
    public List<Venue> Venues { get; set; }
}

public class Venue
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Contact Contact { get; set; }
    public Location Location { get; set; }
    public string CanonicalUrl { get; set; }
    public Categories Categories { get; set; }
    public bool Verified { get; set; }
}

执行请求......

var request = new RestRequest(uri);
var response = client.Execute<Response>(request);

2)忽略meta属性并开始在response属性进行解析。

*另外,看起来JSON响应的meta属性可能是HTTP状态代码。如果是,您仍然需要它,RestSharp也为您提供(见下文)。

public class Response
{
    public string Meta { get; set; }
    public List<Venue> Venues { get; set; }
}

public class Venue
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Contact Contact { get; set; }
    public Location Location { get; set; }
    public string CanonicalUrl { get; set; }
    public Categories Categories { get; set; }
    public bool Verified { get; set; }
}

但是,这需要告诉RestSharp从哪里开始解析响应。

var request = new RestRequest(uri)
{
    RootElement = "response"
};
var response = client.Execute<Response>(request);

// and the HTTP status (if that's what you need)
response.StatusCode

答案 2 :(得分:0)

如果我正朝着正确的方向前进,那么你的JSON不是Valid

Error:Strings should be wrapped in double quotes

验证jsonformatter

[增订]

有效的JSON就像: -

{
"meta": {
    "code": 200
        },
    "notifications": 
        [
            {
                "type": "notificationTray",
                "item": {
            "unreadCount": 0
                }
            }
        ],
    "response": {
    "venues": [
        {
            "id": "4e15d1348877cd5712112a44",
            "name": "The Arena",
    "contact": { },
    "location": {
        "address": "110 Wall Street",
        "lat": 40.70432634495503,
        "lng": -74.0055421062419,
        "distance": 671,
        "city": "New York",
        "state": "NY",
        "country": "United States",
        "cc": "US"
    },
    "canonicalUrl": "https://foursquare.com/v/the-arena/4e15d1348877cd5712112a44",
    "categories": [
        {
            "id": "4bf58dd8d48988d1e4941735",
            "name": "Campground",
    "pluralName": "Campgrounds",
    "shortName": "Campground",
    "icon": {
            "prefix": "https://foursquare.com/img/categories_v2/parks_outdoors/campground_",
            "suffix": ".png"
    },
    "primary": true
}
],
"verified": false,
"stats": {
        "checkinsCount": 149,
        "usersCount": 25,
        "tipCount": 4
},
"specials": {
        "count": 0,
        "items": [ ]
},
"hereNow": {
        "count": 0,
        "groups": [ ]
},
"referralId": "v-1366314443"
}         
]
}

}

答案 3 :(得分:0)

对.NET对象的JSON反序列化区分大小写。您的属性名称与JSON标记正确匹配,这就是为什么当您尝试反序列化时,您将返回NULL。