How to deserialize JSON output in C # Struct (based on return from calling the Google Maps API)

The first post you post here is as complete newb, so apologize if I don't post this question correctly.

I have a structure for storing Geo coordinates in a .NET MVC5 project

public struct GeoCoordinate { private readonly double latitude; private readonly double longitude; public double Latitude { get { return latitude; } } public double Longitude { get { return longitude; } } public GeoCoordinate(double latitude, double longitude) { this.latitude = latitude; this.longitude = longitude; } public override string ToString() { return string.Format("{0},{1}", Latitude, Longitude); } } 

I'm trying to call the Post Create method method to populate the coordinate values ​​with the location coordinates provided by the Google Maps API, which provides a back-formatted JSON data stream.

 public GeoCoordinate GEOCodeAddress(String Address) { var address = String.Format("https://maps.googleapis.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); } 

I can "see" that the result contains JSON, which has (among many other attributes) the location coordinates in it that I want to return, I just have problems with the syntax to "parse" the location attributes and return them to my method create.

In the JSON renderer, the coordinates are in JSON [0] .geometry.location.lat and JSON [0] .geometry.location.lng, and I assume (?!) That JSON [0] .geometry. location contains an object that must be compatible (ish) with my structure. I am happy to make changes to the structure to make it compatible if necessary!

Thanks in advance for any help / understanding you can give!

+4
source share
2 answers

Looking at the JSON returned by this URL, the classes you need to deserialize to an object look like this (the easiest way to get this code is to copy the JSON to the clipboard, then in Visual Studio, the Edit menu, Paste Special "and select Paste as JSON Classes ):

 public class RootObject { public Result[] results { get; set; } public string status { get; set; } } public class Result { public Address_Components[] address_components { get; set; } public string formatted_address { get; set; } public Geometry geometry { get; set; } public string place_id { get; set; } public string[] types { get; set; } public bool partial_match { 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 Northeast { public float lat { get; set; } public float lng { get; set; } } public class Southwest { public float lat { get; set; } public float lng { get; set; } } public class Location { public float lat { get; set; } public float lng { get; set; } } public class Viewport { public Northeast1 northeast { get; set; } public Southwest1 southwest { get; set; } } public class Northeast1 { public float lat { get; set; } public float lng { get; set; } } public class Southwest1 { public float lat { get; set; } public float lng { get; set; } } public class Address_Components { public string long_name { get; set; } public string short_name { get; set; } public string[] types { get; set; } } 

And to get this data into one of these objects, just do it (using Json.Net):

 RootObject googleData = JsonConvert.DeserializeObject<RootObject>(result); 
+3
source

Hi, after some experimentation, I finally realized that I could parse it using the following syntax. There is most likely a less detailed version, but it works!

  public GeoCoordinate GEOCodeAddress(String Address) { var address = String.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=false", Address.Replace(" ", "+")); var result = new System.Net.WebClient().DownloadString(address); JavaScriptSerializer jss = new JavaScriptSerializer(); RootObject googleData = JsonConvert.DeserializeObject<RootObject>(result); GeoCoordinate coordinates = new GeoCoordinate(); coordinates.Latitude= googleData.results[0].geometry.location.lat; coordinates.Longitude= googleData.results[0].geometry.location.lng; return coordinates; 
0
source

All Articles