a developer's notes – a semi-technical web development BLOG

September 12, 2013

Parsing Google Maps API V3 JSON Response in C# .NET 4

Filed under: ASP.NET MVC,C# — Duy Nguyen @ 10:53 pm
Tags: , , , , , , , , , ,

Here is what you will need to parse responses from Google Maps V3.

Request to the following URL:
http://maps.googleapis.com/maps/api/geocode/json?address=1000+broad+street+road%2c+short+pump%2c+VA+23233&sensor=false

Reponse:

{
   'results' : [
	  {
		 'address_components' : [
			{
			   'long_name' : 'Broad Street Road',
			   'short_name' : 'Broad Street Rd',
			   'types' : [ 'route' ]
			},
			{
			   'long_name' : 'Virginia',
			   'short_name' : 'VA',
			   'types' : [ 'administrative_area_level_1', 'political' ]
			},
			{
			   'long_name' : 'United States',
			   'short_name' : 'US',
			   'types' : [ 'country', 'political' ]
			}
		 ],
		 'formatted_address' : 'Broad Street Road, VA, USA',
		 'geometry' : {
			'bounds' : {
			   'northeast' : {
				  'lat' : 37.6689969,
				  'lng' : -77.64866649999999
			   },
			   'southwest' : {
				  'lat' : 37.662306,
				  'lng' : -77.6778078
			   }
			},
			'location' : {
			   'lat' : 37.6657544,
			   'lng' : -77.66309579999999
			},
			'location_type' : 'GEOMETRIC_CENTER',
			'viewport' : {
			   'northeast' : {
				  'lat' : 37.6689969,
				  'lng' : -77.64866649999999
			   },
			   'southwest' : {
				  'lat' : 37.662306,
				  'lng' : -77.6778078
			   }
			}
		 },
		 'partial_match' : true,
		 'types' : [ 'route' ]
	  }
   ],
   'status' : 'OK'
}

These are the classes you need to define so you can deserialize the json into a type object.

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

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

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

public class bounds
{
	public northeast northeast { get; set; }
	public southwest southwest { get; set; }
}

public class viewport
{
	public northeast northeast { get; set; }
	public southwest southwest { get; set; }
}

public class northeast : location
{
}

public class southwest : location
{
}

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

public class address_component
{
	public string long_name { get; set; }
	public string short_name { get; set; }
	public string[] types { get; set; }
}

Here is the code you can use to decode your response.

private void GoogleJsonDecode()
{
	string response = @"
						{
						   'results' : [
							  {
								 'address_components' : [
									{
									   'long_name' : 'Broad Street Road',
									   'short_name' : 'Broad Street Rd',
									   'types' : [ 'route' ]
									},
									{
									   'long_name' : 'Virginia',
									   'short_name' : 'VA',
									   'types' : [ 'administrative_area_level_1', 'political' ]
									},
									{
									   'long_name' : 'United States',
									   'short_name' : 'US',
									   'types' : [ 'country', 'political' ]
									}
								 ],
								 'formatted_address' : 'Broad Street Road, VA, USA',
								 'geometry' : {
									'bounds' : {
									   'northeast' : {
										  'lat' : 37.6689969,
										  'lng' : -77.64866649999999
									   },
									   'southwest' : {
										  'lat' : 37.662306,
										  'lng' : -77.6778078
									   }
									},
									'location' : {
									   'lat' : 37.6657544,
									   'lng' : -77.66309579999999
									},
									'location_type' : 'GEOMETRIC_CENTER',
									'viewport' : {
									   'northeast' : {
										  'lat' : 37.6689969,
										  'lng' : -77.64866649999999
									   },
									   'southwest' : {
										  'lat' : 37.662306,
										  'lng' : -77.6778078
									   }
									}
								 },
								 'partial_match' : true,
								 'types' : [ 'route' ]
							  }
						   ],
						   'status' : 'OK'
						}";

	JavaScriptSerializer jss = new JavaScriptSerializer();
	GoogleGeoCodeResponse test = jss.Deserialize<GoogleGeoCodeResponse>(response);
}

Now you have this!

googleGeoCodeObj

Advertisements

3 Comments »

  1. Is this your source http://stackoverflow.com/a/3389222/1266056?

    Comment by Anonymous — January 24, 2014 @ 4:54 am | Reply

  2. cool

    Comment by Anonymous — January 28, 2016 @ 7:24 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: