我错过了什么?RestSharp无法反序列化Json。

10

我试图将来自Foursquare的JSON响应转换为对象。 我得到的大致如下:

   {
   "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始终为null。我不确定原因。


chobo2,你的JSON无效,并且在字段之间缺少许多逗号。因此RetSharp是正确的。 - I4V
假设JSON是有效的。它来自Foursquare(它必须工作……)。您所描述的问题是因为我从他们的实时工具中复制了它,该工具显然会剥离引号……通过代码发送请求时需要引号。 - chobo2
3个回答

3

您只需在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

这个答案解决了原帖的核心问题。 - wlf

0

如果我走的方向是正确的,那么你的 JSON 不是 有效的

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"
}         
]
}

}

正确,但它是有效的JSON。我使用了他们的在线工具返回JSON结果(https://developer.foursquare.com/docs/explore#req=venues/search%3Fll%3D40.7,-74%26groceryStoreId%3D4bf58dd8d48988d118951735),似乎去掉了引号。我重新发布了我的代码获取的内容。 - chobo2

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接