Randy Minder
Randy Minder

Reputation: 48492

How to extract a value from JSON using jSON.NET and JObject.Parse

I have a very simple C# program that makes a URL call and retrieves a jSON string. This works fine.

static void Main(string[] args)
{
    using (var webClient = new System.Net.WebClient())
    {
        var json = webClient.DownloadString("http://maps.google.com/maps/api/geocode/json?address=Oak%20Openings%20Metro%20Park%20Ohio&sensor=false);

        var obj = JObject.Parse(json);
    }
}

The JSON returned looks as follows:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Oak Openings Preserve Metropark",
               "short_name" : "Oak Openings Preserve Metropark",
               "types" : [ "establishment" ]
            },
            {
               "long_name" : "Girdham Road",
               "short_name" : "Girdham Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Swanton",
               "short_name" : "Swanton",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "43558",
               "short_name" : "43558",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Oak Openings Preserve Metropark, Girdham Road, Swanton, OH 43558, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.5828434,
                  "lng" : -83.82480509999999
               },
               "southwest" : {
                  "lat" : 41.5286092,
                  "lng" : -83.8826873
               }
            },
            "location" : {
               "lat" : 41.5517744,
               "lng" : -83.85260359999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.5828434,
                  "lng" : -83.82480509999999
               },
               "southwest" : {
                  "lat" : 41.5286092,
                  "lng" : -83.8826873
               }
            }
         },
         "partial_match" : true,
         "types" : [ "park", "establishment" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Oak Openings Metropark Lodge",
               "short_name" : "Oak Openings Metropark Lodge",
               "types" : [ "point_of_interest", "establishment" ]
            },
            {
               "long_name" : "5230",
               "short_name" : "5230",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Wilkins Road",
               "short_name" : "Wilkins Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Whitehouse",
               "short_name" : "Whitehouse",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Swanton",
               "short_name" : "Swanton",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Lucas County",
               "short_name" : "Lucas County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "43571",
               "short_name" : "43571",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Oak Openings Metropark Lodge, 5230 Wilkins Road, Whitehouse, OH 43571, USA",
         "geometry" : {
            "location" : {
               "lat" : 41.5483617,
               "lng" : -83.8395838
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.5497106802915,
                  "lng" : -83.83823481970849
               },
               "southwest" : {
                  "lat" : 41.5470127197085,
                  "lng" : -83.84093278029151
               }
            }
         },
         "partial_match" : true,
         "types" : [ "park", "point_of_interest", "establishment" ]
      }
   ],
   "status" : "OK"
}

I am very new to JSON, I would like to extract the lat and lng values from this piece:

"location": {
              "lat": 27.9094665,
              "lng": -82.7873244
            },

but really have no idea how to go about it after I've called JObject.Parse.

Upvotes: 0

Views: 4999

Answers (2)

Marek Woźniak
Marek Woźniak

Reputation: 1786

You can use LINQ to JSON. Your path to location's:

results/geometry/location/lat
results/geometry/location/lng

well:

var response = webClient.DownloadString("http://maps.google.com/maps/api/geocode/json?address=Oak%20Openings%20Metro%20Park%20Ohio&sensor=false);

now select location:

JObject json = JObject.Parse(response);

int lat = (int)json["results"]["geometry"]["location"]["lat"];
int lng = (int)json["results"]["geometry"]["location"]["lng"];

here's documentation for your problem: http://james.newtonking.com/json/help/index.html?topic=html/M_Newtonsoft_Json_JsonConvert_DeserializeObject__1.htm

http://james.newtonking.com/json/help/index.html?topic=html/QueryingLINQtoJSON.htm

P.S I can't try it now, but i think that it can help you or give a way to solution. Please search for nodeJson, it's worth for your problem

Upvotes: 3

user1228
user1228

Reputation:

It's relatively simple. You're using a different part of the framework than I usually use, so I'm not exactly sure how to do it using the JObject and other J-types. I believe they are useful when using dynamic objects, in which case you'd just index into the dynamic object via properties. Or via the framework, am not sure.

What I normally do is create POCO object representations of the json object and then use JsonConvert.DeserializeObject<T> to deserialize the json into something I can use in the application.

Upvotes: 1

Related Questions