2

Given:

The classes:

public class Venue
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    [JsonProperty("location")]
    public Location Location { get; set; }
}

public class Location
{
    public long Lat { get; set; }
    public long Lng { get; set; }
    public int Distance { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
}

public class Response
{
    private List<Venue> _venues;
    [JsonProperty("venues")]
    public List<Venue> Venues
    {
        get { return _venues ?? (_venues = new List<Venue>()); }
        set { _venues = value; }
    }
}

And a response request:

   {
    "meta":{
      "code":200
   },
   "response":{
      "venues":[
         {
            "id":"4f96a5aee4b01cb74e4dc3c6",
            "name":"Centro",
            "contact":{
            },
            "location":{
               "address":"Centro",
               "lat":-21.256906640441052,
               "lng":-48.31978432813259,
               "distance":185,
               "city":"Jaboticabal",
               "state":"SP",
               "country":"Brazil",
               "cc":"BR"
            },
            "canonicalUrl":"https:\/\/foursquare.com\/v\/centro\/4f96a5aee4b01cb74e4dc3c6",
            "categories":[
               {
                  "id":"4f2a25ac4b909258e854f55f",
                  "name":"Neighborhood",
                  "pluralName":"Neighborhoods",
                  "shortName":"Neighborhood",
                  "icon":{
                     "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/parks_outdoors\/neighborhood_",
                     "suffix":".png"
                  },
                  "primary":true
               }
            ],
            "verified":false,
            "restricted":true,
            "stats":{
               "checkinsCount":1106,
               "usersCount":86,
               "tipCount":0
            },
            "specials":{
               "count":0,
               "items":[
               ]
            },
            "hereNow":{
               "count":0,
               "groups":[
               ]
            },
            "referralId":"v-1376511204"
         },
         {
            "id":"4c38b0b21a38ef3b56b39221",
            "name":"Ice by Nice",
            "contact":{
            },
            "location":{
               "address":"Jaboticabal Shopping",
               "lat":-21.25513775,
               "lng":-48.32320093,
               "distance":253,
               "city":"Jaboticabal",
               "state":"SP",
               "country":"Brazil",
               "cc":"BR"
            },
            "canonicalUrl":"https:\/\/foursquare.com\/v\/ice-by-nice\/4c38b0b21a38ef3b56b39221",
            "categories":[
               {
                  "id":"4bf58dd8d48988d1c9941735",
                  "name":"Ice Cream Shop",
                  "pluralName":"Ice Cream Shops",
                  "shortName":"Ice Cream",
                  "icon":{
                     "prefix":"https:\/\/foursquare.com\/img\/categories_v2\/food\/icecream_",
                     "suffix":".png"
                  },
                  "primary":true
               }
            ],
            "verified":false,
            "restricted":true,
            "stats":{
               "checkinsCount":656,
               "usersCount":309,
               "tipCount":15
            },
            "specials":{
               "count":0,
               "items":[
               ]
            },
            "hereNow":{
               "count":2,
               "groups":[
                  {
                     "type":"others",
                     "name":"Other people here",
                     "count":2,
                     "items":[
                     ]
                  }
               ]
            },
            "referralId":"v-1376511204"
         }
      ]
   }
}

When using JSON.NET like so:

Response response = JsonConvert.DeserializeObject<Response>(jsonString);

The deserialized response object has a empty list of venues, What am I doing wrong?

Thanks

4
  • put your json to json2csharp.com and tada.... you got your classes.... Commented Aug 14, 2013 at 21:09
  • @I4V I actually don't recommend using those tools. I had a guy in my group do it for generating objects (and also a similar thing for making schemas) the stuff it generated was total shit. It doesn't understand composition and kept redefining the same objects but tacking an integer on the end of the type name to make it unique. I just ended up having to go back and rewrite all his code. Commented Aug 14, 2013 at 21:12
  • @evanmcdonnal If it generates a shit, then don't use it. What is wrong knowing a tool and utilize it if it suits to your needs. Your comment is valid for any tool and only occupies space.... Commented Aug 14, 2013 at 21:22
  • @I4V fair enough. I'm just pointing out that, in my experience, that tool has only generated useful code for the most trivial json. Commented Aug 14, 2013 at 21:25

1 Answer 1

4

There's a bit of a mismatch in what you're trying to deserialize and the class you have defined to deserialize into. You unfortunately need yet another layer of indirection. Note the response has;

// theres an outer object here which contains response
{
     "meta":{ "code":200 }, 
     "response":{
         // you're trying to deserialize this, but it's not the entire response, it's a property of an anonymous object
     }
}

So if I make a new class;

public class ResponseWrapper
{
      public object meta;
      public Response response;
}

And instead do;

ResponseWrapper response = JsonConvert.DeserializeObject<ResponseWrapper>(jsonString);

Then it will work.

Note that when you're deserializing using json.NET you have to define a structure that exactly matches the json. In this case you're leaving out the outer most object. It is kind of an annoyance and leads to a lot of code like what I just wrote but that's just the way it goes sometimes.

Sign up to request clarification or add additional context in comments.

1 Comment

I thought that was the problem, but was hoping that it wasn't true. It worked perfectly and I will mark as an answer as soon as the system allows me. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.