10

我正在尝试将 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

3 回答 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属性。

*顺便说一句,看起来metaJSON 响应的属性可能是 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
于 2016-02-25T16:58:21.200 回答
0

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

于 2013-04-20T23:53:31.963 回答
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"
}         
]
}

}
于 2013-04-18T20:13:31.823 回答