troubleshooting Question

How best to parse a JSON string from Foursquare API into C# Object

Avatar of chongchian
chongchianFlag for Singapore asked on
JavaScriptASP.NETScripting Languages
2 Comments1 Solution3697 ViewsLast Modified:
Hi,

I am trying to parse a JSON response from Foursquare API:.

The example is in this link:

https://developer.foursquare.com/docs/explore#req=venues/search?ll=40.7,-74&query=starbucks

It returns me the following:

{
meta: {
code: 200
}
notifications: [
{
type: "notificationTray"
item: {
unreadCount: 8
}
}
]
response: {
venues: [
{
id: "49edd0a5f964a520fa671fe3"
name: "Starbucks"
contact: {
phone: "7182430455"
formattedPhone: "(718) 243-0455"
twitter: "Starbucks"
}
location: {
address: "112 Montague St"
crossStreet: "btw Hicks & Henry"
lat: 40.69500532
lng: -73.994969
distance: 699
postalCode: "11201"
city: "Brooklyn"
state: "NY"
country: "United States"
}
categories: [
{
id: "4bf58dd8d48988d1e0931735"
name: "Coffee Shop"
pluralName: "Coffee Shops"
shortName: "Coffee Shop"
icon: {
prefix: "https://foursquare.com/img/categories/food/coffeeshop_"
sizes: [
32
44
64
88
256
]
name: ".png"
}
primary: true
}
]
verified: true
stats: {
checkinsCount: 3391
usersCount: 1186
tipCount: 25
}
url: "http://www.starbuckscoffee.com/"
specials: {
count: 0
items: [ ]
}
hereNow: {
count: 0
}
menu: {
url: "https://foursquare.com/v/starbucks/49edd0a5f964a520fa671fe3/menu"
mobileUrl: "https://foursquare.com/v/49edd0a5f964a520fa671fe3/device_menu"
}

}

What I would like to do is to parse the JSON, traverse through it, and get the element values .

For example:

name=Starbucks
contact: {
phone: "7182430455"
formattedPhone: "(718) 243-0455"
twitter: "Starbucks"}


This is a sample code i wrote:

using (JsonTextReader reader = new JsonTextReader(new StringReader(returnString)))
                       {
                           while (reader.Read())
                           {
                               if (reader.TokenClass == JsonTokenClass.String &&
                                   reader.Text.Contains("name"))
                               {
                                  //return the name value

                                   venueInfoListBox.Items.Add(reader.Text);
                               }
                           }
                       }


I've experimented with JsonTextReader in the above code and tried to retrieve the value of the name (Starbucks), and am able to retrieve all entries with starbucks. However, my painpoint is to find an efficient way to retrieve the value of each element from the JSON string such as name, contact etc. so that i can map to a C# object.

What's the best way to go about this?

thank you!
ASKER CERTIFIED SOLUTION
MlandaT

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros