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

标签 c# json foursquare restsharp

我正在尝试将来自 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 始终为空。我不确定为什么。

最佳答案

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

关于c# - 我错过了什么? RestSharp 不会反序列化 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092048/

相关文章:

c# - 如何按位置替换字符串的一部分?

JSON Schema - 基于所选值的下拉列表其他下拉列表

json - 使用 SwiftyJSON 和 Alamofire 在 TableView 上显示 JSON 数据

c# - 无需声明对象即可将 JSON 数据传递给 Controller ​​方法

c# - 统一: How to save and load scenes according to player progress

python - 使用 python (foursquare API) 解析 json 文件中的较低级别?

javascript - 如何从 foursquare field 提示 api url 获取 JSON 数据并以 HTML 格式制作

ios - 编译 iOS 应用程序时使用 armv7 对性能有何影响?

c# - 以编程方式使用选中的选项填充 CheckBoxList

ruby-on-rails - rails : Disable root in JSON for only specific controller actions?