【问题标题】:What am I missing? RestSharp won't deserialize Json我错过了什么? RestSharp 不会反序列化 Json
【发布时间】:2013-04-18 20:02:43
【问题描述】:

我正在尝试将 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 始终为空。我不知道为什么。

【问题讨论】:

  • chobo2,您的 json 无效并且字段之间缺少许多逗号。所以 RetSharp 是对的。
  • 假设 json 有效。它来自foursquare(它必须工作......)。您描述的问题是因为我从他们的实时工具中复制了它,显然去掉了引号......通过代码的请求有引号。

标签: c# json foursquare restsharp


【解决方案1】:

您只需要更深入地了解 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

【讨论】:

  • 这个答案解决了OP的核心问题。
【解决方案2】:

如果我朝着正确的方向前进,那么你的 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】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多