3

I have my code working fine, but I can't seem to be able to get to the deeper parts of the tree. I'm trying to pull the longitude and the latitude. The code below pulls 'status' no problem as 'OK" (at the very end of the response). What is the syntax for 'geometry' -> 'location' -> 'lat' and 'lng'?

Here is my code:

string RawAddress = "163 Leektown Road, New Gretna, NJ 08004";
string Address = RawAddress.Replace(" ", "+");
string AddressURL = "http://maps.google.com/maps/api/geocode/json?address=" + Address;
var result = new System.Net.WebClient().DownloadString(AddressURL);
dynamic data = JObject.Parse(result);

Lat.Text = data.status;

This is what the API generates:

{
   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "Mountain View",
           "short_name" : "Mountain View",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Santa Clara County",
           "short_name" : "Santa Clara County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "California",
           "short_name" : "CA",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Mountain View, CA, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 37.4508789,
              "lng" : -122.0446721
           },
           "southwest" : {
              "lat" : 37.3567599,
              "lng" : -122.1178619
           }
        },
        "location" : {
           "lat" : 37.3860517,
           "lng" : -122.0838511
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 37.4508789,
              "lng" : -122.0446721
           },
           "southwest" : {
              "lat" : 37.3567599,
              "lng" : -122.1178619
           }
        }
     },
     "partial_match" : true,
     "types" : [ "locality", "political" ]
  }
  ],
  "status" : "OK"
  }
Kory D
  • 33
  • 1
  • 4
  • possible duplicate of [How to parse json in C#?](http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c) – MethodMan Feb 06 '15 at 17:34
  • Try pasting the Json into this page: http://json2csharp.com/ – Lasse V. Karlsen Feb 06 '15 at 17:37
  • MethodMan, thanks for that. I've seen that, but it didn't help. Party because I don't know what namespaces I'm supposed to use. I'm happy with what I posted, because it actually works. – Kory D Feb 06 '15 at 18:35
  • Lasse V. Karlsen, thanks for that too, but I can't make heads or tails of it. How do I plug those in? – Kory D Feb 06 '15 at 18:36

2 Answers2

18

Here are the steps to get what you want:

  1. Post your JSON in http://json2csharp.com/. Take the resulting classes and merge duplicates, and you get:

    public class AddressComponent
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public List<string> types { get; set; }
    }
    
    public class Bounds
    {
        public Location northeast { get; set; }
        public Location southwest { get; set; }
    }
    
    public class Location
    {
        public double lat { get; set; }
        public double lng { get; set; }
    }
    
    public class Geometry
    {
        public Bounds bounds { get; set; }
        public Location location { get; set; }
        public string location_type { get; set; }
        public Bounds viewport { get; set; }
    }
    
    public class Result
    {
        public List<AddressComponent> address_components { get; set; }
        public string formatted_address { get; set; }
        public Geometry geometry { get; set; }
        public bool partial_match { get; set; }
        public List<string> types { get; set; }
    }
    
    public class RootObject
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }
    

    (You could also use Paste JSON as Classes or https://jsonutils.com/ to generate your initial type definitions.)

  2. Deserialize your JSON with Json.NET like so:

        var root = JsonConvert.DeserializeObject<RootObject>(result);
    
  3. There are multiple results returned for your query, so you need to loop through the locations returned like so:

        foreach (var singleResult in root.results)
        {
            var location = singleResult.geometry.location;
            var latitude = location.lat;
            var longitude = location.lng;
            // Do whatever you want with them.
        }
    
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Worked like a charm. Thanks for spelling it out like that. The only thing I would add is the exact namespace to make this work: Newtonsoft.Json. – Kory D Feb 06 '15 at 20:21
  • Great answer. Didn't know the existence of `http://json2csharp.com/`. Very useful! – Felipe Cruz Apr 11 '16 at 08:00
1

The syntax for 'geometry' -> 'location' -> 'lat' and 'lng' is:

JObject data = JObject.Parse(result);

string lat = (string)data["results"][0]["geometry"]["location"]["lat"];
string lng = (string)data["results"][0]["geometry"]["location"]["lng"];