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);
source share