What am I missing? RestSharp won't deserialize Json

19,440

You simply need to go a level deeper in the JSON response. One level up from the venues property is the response property, which is not currently represented in your Response class.

You have two ways to solve this.

1) Add another wrapping response object, which contains 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 executing the request...

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

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

*As an aside, it looks like the meta property of the JSON response might be an HTTP status code. If it is and you still need it, RestSharp provides that for you as well (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, this will require telling 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's what you need)
response.StatusCode
Share:
19,440
chobo2
Author by

chobo2

Updated on September 15, 2022

Comments

  • chobo2
    chobo2 over 1 year

    I am trying to turn a json response back from foursquare into 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 a class like this

     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;
    

    Yet Venues is always null. I am not sure why though.

  • chobo2
    chobo2 about 11 years
    right but it is valid json. It was that I took the results from their online tool that returns the json back(developer.foursquare.com/docs/explore#req=venues/…) what seems to strip away the quotes. I re-posted with what I am getting back in my code.
  • wlf
    wlf about 7 years
    This answer addresses the core issue of the OP.