Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a DB full of addresses I need to get lat and long for, so I want to loop through them and use Google Geocode to update my database. I am stuck as to how to parse the JSOn result to get what I need:

var address = "http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";
var result = new System.Net.WebClient().DownloadString(address);
GoogleGeoCodeResponse test = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(result);

I thought I could simply build a quick class and use JSON.Net to deserialize the result, and it is kind of working but I think I am blowing it on my class structure:

public  class GoogleGeoCodeResponse {

    public string status { get; set; }
    public geometry geometry { get; set; }

}

public class geometry {
    public string location_type { get; set; }
    public location location { get; set; }
}

public class location {
    public string lat {get;set;}
    public string lng {get;set;}
}

Here is a sample of what get's returned from Google:

{
  "status": "OK",
  "results": [ {
    "types": [ "street_address" ],
    "formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
    "address_components": [ {
      "long_name": "1600",
      "short_name": "1600",
      "types": [ "street_number" ]
    }, {
      "long_name": "Amphitheatre Pkwy",
      "short_name": "Amphitheatre Pkwy",
      "types": [ "route" ]
    }, {
      "long_name": "Mountain View",
      "short_name": "Mountain View",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "California",
      "short_name": "CA",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "United States",
      "short_name": "US",
      "types": [ "country", "political" ]
    }, {
      "long_name": "94043",
      "short_name": "94043",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 37.4219720,
        "lng": -122.0841430
      },
      "location_type": "ROOFTOP",
      "viewport": {
        "southwest": {
          "lat": 37.4188244,
          "lng": -122.0872906
        },
        "northeast": {
          "lat": 37.4251196,
          "lng": -122.0809954
        }
      }
    }
  } ]
}

I am missing simple here I know it, anyone?

share|improve this question
add comment (requires an account with 50 reputation)

3 Answers

up vote 17 down vote accepted

I tried this, made a simple test and it worked (added results and other):

public class GoogleGeoCodeResponse
{

    public string status { get; set; }
    public results[] results { get; set; }

}

public class results
{
    public string formatted_address { get; set; }
    public geometry geometry { get; set; }
    public string[] types { get; set; }
    public address_component[] address_components { get; set; }
}

public class geometry
{
    public string location_type { get; set; }
    public location location { get; set; }
}

public class location
{
    public string lat { get; set; }
    public string lng { get; set; }
}

public class address_component
{
    public string long_name { get; set; }
    public string short_name { get; set; }
    public string[] types { get; set; }
}
share|improve this answer
1  
Your data structure works for me, but I prefer defining location.lat and location.lng as decimals. Likewise is it easier to have enums for the response status, result types and location types. Json.NET will deserialize strings to enums correctly. – Mart Aug 26 '10 at 5:44
This line "public results[] results { get; set; }" solved a two hour roadblock. I never thought of putting another class a level above the results class. Thank you. – TrueGuidance Feb 10 at 0:58
add comment (requires an account with 50 reputation)

You can use a dynamic object rather than defining the object.

 public static dynamic GEOCodeAddress(String Address)
    {
        var address = String.Format("http://maps.google.com/maps/api/geocode/json?address={0}&sensor=false", Address.Replace(" ", "+"));
        var result = new System.Net.WebClient().DownloadString(address);
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<dynamic>(result);
    }
share|improve this answer
namespace is -> System.Web.Script.Serialization.JavaScriptSerializer in System.Web.Extensions (in System.Web.Extensions.dll) – JGilmartin Mar 7 '12 at 14:38
I love the simplicity of this. – Nick Devereaux May 4 '12 at 2:14
Beautiful work.. I tried this, works fine with .NET Framework 4(Microsoft.Csharp.dll); whereas not working in .NET Framework 3.5. Any idea? – user1671639 Apr 2 at 8:35
Thanks all, I feel all warm and fuzzy. @user1671639 DownloadString() came in with .net 4.5 you can use other methods to get the result of the web call using other "Get a Web Page as a String" methods ... the same is true for the JavaScriptSerializer .... however there is an open source JSON implementation for C# out there also. – matt.j.crawford May 20 at 4:53
@matt.j.crawford Thanks. One suggestion, please mention this in your answer. Would be helpful to others. – user1671639 May 20 at 6:11
add comment (requires an account with 50 reputation)

i have done something similar refer to Google Geo Kit

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.