How to parse response with Google Maps API using JSON?

I want to calculate the distance between points in C # with the google map APIs.

I use the following code to query:

private void MapsAPICall() { //Pass request to google api with orgin and destination details HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://maps.googleapis.com/maps/api/distancematrix/json?origins=" + "51.123959,3.326682" + "&destinations=" + "51.158089,4.145267" + "&mode=Car&language=us-en&sensor=false"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (var streamReader = new StreamReader(response.GetResponseStream())) { var result = streamReader.ReadToEnd(); if (!string.IsNullOrEmpty(result)) { Distance t = JsonConvert.DeserializeObject<Distance>(result); } } } 

And then I want to parse the json answer in the Distance class:

 public struct Distance { // Here I want to parse the distance and duration } 

Here is an example json response I get: http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC&destinations=San+Francisco&mode=bicycling&language=fr-FR&sensor=false

How to parse distance and duration in class Distance?
This is the first time I use Json, so I do not experience this.

Ps: I have installed the json.net library.

+4
source share
4 answers

Web Essentials has the ability to "Insert JSON as classes." This will create the classes needed to deserialize your JSON.

Using this parameter, you will create the following for yourself:

 public class Distance { public string text { get; set; } public int value { get; set; } } public class Duration { public string text { get; set; } public int value { get; set; } } public class Element { public Distance distance { get; set; } public Duration duration { get; set; } public string status { get; set; } } public class Row { public Element[] elements { get; set; } } public class Parent { public string[] destination_addresses { get; set; } public string[] origin_addresses { get; set; } public Row[] rows { get; set; } public string status { get; set; } } 

(If you want, you can reorganize the generated code to make it more readable. You will need to add Json.NET attributes to make sure serialization is still working)

Then in your code, you can use the following line to deserialize the code:

 Parent result = JsonConvert.DeserializeObject<Parent>(json); 
+5
source

Update for Visual Studio 2015 +

As a complement to Wouter de Kort, a useful answer is this update for Visual Studio 2015+.

Visual Studio 2015+ has such a convenient "Insert JSON as classes" that Wouter kindly described.

To create the necessary classes for deserializing the JSON API of GoogleMaps:

  • In a web browser or REST client, navigate to the GoogleMaps API URL with some testing options. For example: https://maps.googleapis.com/maps/api/distancematrix/json?origins=30.3449153,-81.860543&destinations=33.7676932,-84.4906437&language=en-US&units=imperial&key=<API_KEY>
  • Copy ( Ctrl+C ) the resulting raw JSON response from your browser / client to the clipboard.
  • In Visual Studio 2015+, open the .cs file in which you would like to create classes.
  • Place the cursor in the .cs file where you would like to generate the classes.
  • From the Visual Studio menu, choose Edit> Paste Special> Paste JSON As Classes.

Example Resulting Classes

 public class Rootobject { public string[] destination_addresses { get; set; } public string[] origin_addresses { get; set; } public Row[] rows { get; set; } public string status { get; set; } } public class Row { public Element[] elements { get; set; } } public class Element { public Distance distance { get; set; } public Duration duration { get; set; } public string status { get; set; } } public class Distance { public string text { get; set; } public int value { get; set; } } public class Duration { public string text { get; set; } public int value { get; set; } } 

From now on, you can follow the rest of the Wouter tips.

 Rootobject result = JsonConvert.DeserializeObject<Rootobject>(json); 
+2
source

Late, but may be useful to someone. In such cases, when we have a JSON string and want to have the proper class structure to deserialize it correctly, you can use the online tool http://json2csharp.com/

Just insert the JSON string and click the "Create" button. It will provide you with the necessary class structure. It is so simple. This approach is useful if you want to avoid installing external tools.

0
source

The Distance type should correctly model the JSON schema, for example:

 public string[] destination_addresses { get; set; } public string[] origin_addresses { get; set; } public Row[] rows { get; set; } public string status { get; set; } 

(and, of course, subtypes like Row should also be modeled)

Other than that, using Json.Net:

 Distance t = JsonConvert.DeserializeObject<Distance>(result); 

should be ok

-1
source

All Articles