10

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
  • 75,304
  • 170
  • 472
  • 780
  • chobo2, your json is invalid and missing many commas between fields. So RetSharp is right. – I4V Apr 18 '13 at 20:22
  • Assume that json is valid. It is coming from foursquare(it must work....). The problem you are describing was because I copied it from their live tool what apparently strips out the quotes...the request through code has quotes. – chobo2 Apr 18 '13 at 20:25

3 Answers3

2

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
Ryan V
  • 470
  • 3
  • 13
0

If i am going in right direction then, ur JSON is not Valid

Error:Strings should be wrapped in double quotes

Get it validated jsonformatter

[UPDATED]

Valid JSON would be like:-

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

}
Shubh
  • 6,279
  • 8
  • 42
  • 74
  • right but it is valid json. It was that I took the results from their online tool that returns the json back(https://developer.foursquare.com/docs/explore#req=venues/search%3Fll%3D40.7,-74%26groceryStoreId%3D4bf58dd8d48988d118951735) what seems to strip away the quotes. I re-posted with what I am getting back in my code. – chobo2 Apr 18 '13 at 20:19
0

JSON deserialization to .NET objects is case sensative. Your property names don't match the JSON tags properly, and that is why when you attempt to deserialize, you are getting back NULL.

JoshMachol
  • 19
  • 4