What am I missing? RestSharp will not deserialize Json

I am trying to return json response from foursquare in objects. I get something like this back

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

I made such a class

  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; 

However, Venues always zero. However, I do not know why.

+7
source share
4 answers

Ok, I found this great tool http://json2csharp.com/ that converts json to objects. I found this, I needed another package to make it work.

+17
source

You just need to go deeper into the JSON response. One level from the venues property is the response property, which is not currently represented in your response class.

You have two ways to solve this problem.

1) Add another hyphenation response object containing the missing response property

 // 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; } } 

And the execution of the request ...

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

2) Ignore the meta property and start parsing with the response property.

* Aside, it looks like the meta property of the JSON response may be an HTTP status code. If so, and you still need it, RestSharp also provides this for you (see below).

 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; } } 

However, for this you need to tell RestSharp where to start parsing the response.

 var request = new RestRequest(uri) { RootElement = "response" }; var response = client.Execute<Response>(request); // and the HTTP status (if that what you need) response.StatusCode 
+1
source

If I go in the right direction then ur json is not Valid

 Error:Strings should be wrapped in double quotes 

Get this jsonformatter confirmation

[UPDATED]

Valid JSON will look like this: -

 { "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" } ] } } 
0
source

JSON deserialization for .NET objects is case sensitive. Your property names do not match the JSON tags properly, so when you try to deserialize, you return NULL.

0
source

All Articles