You can use Httpwebrequest to get the json from the page and then treat this json using Newtonsoft, throwing the entire json inside an object and then taking only the part you will use like this:
Httpwebrequest code to catch json:
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader sr = null;
Encoding encoding = null;
CookieContainer cookie = new CookieContainer();
string html = string.Empty;
request = (HttpWebRequest)WebRequest.Create("https://maps.googleapis.com/maps/api/directions/json?origin=-22.8895625,-47.0714089&destination=-22.892376,-47.027553&key=");
request.CookieContainer = cookie;
request.Timeout = 30000;
request.Method = "GET";
request.KeepAlive = true;
response = (HttpWebResponse)request.GetResponse();
if (response.CharacterSet != null)
{
encoding = Encoding.GetEncoding(response.CharacterSet);
}
else
{
encoding = System.Text.Encoding.GetEncoding(1252);
}
sr = new StreamReader(response.GetResponseStream(), encoding);
html = sr.ReadToEnd();
You can also add a proxy to this request which is recommended.
To transfer the json text that is in the html variable to the json object you can use this code:
var json = JsonConvert.DeserializeObject<Rootobject>(html);
It will use the Rootobject class that was created from the entire json of the google page :
public class Rootobject
{
public Geocoded_Waypoints[] geocoded_waypoints { get; set; }
public Route[] routes { get; set; }
public string status { get; set; }
}
public class Geocoded_Waypoints
{
public string geocoder_status { get; set; }
public string place_id { get; set; }
public string[] types { get; set; }
}
public class Route
{
public Bounds bounds { get; set; }
public string copyrights { get; set; }
public Leg[] legs { get; set; }
public Overview_Polyline overview_polyline { get; set; }
public string summary { get; set; }
public object[] warnings { get; set; }
public object[] waypoint_order { 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 Overview_Polyline
{
public string points { get; set; }
}
public class Leg
{
public Distance distance { get; set; }
public Duration duration { get; set; }
public string end_address { get; set; }
public End_Location end_location { get; set; }
public string start_address { get; set; }
public Start_Location start_location { get; set; }
public Step[] steps { get; set; }
public object[] traffic_speed_entry { get; set; }
public object[] via_waypoint { 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; }
}
public class End_Location
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Start_Location
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Step
{
public Distance1 distance { get; set; }
public Duration1 duration { get; set; }
public End_Location1 end_location { get; set; }
public string html_instructions { get; set; }
public Polyline polyline { get; set; }
public Start_Location1 start_location { get; set; }
public string travel_mode { get; set; }
public string maneuver { get; set; }
}
public class Distance1
{
public string text { get; set; }
public int value { get; set; }
}
public class Duration1
{
public string text { get; set; }
public int value { get; set; }
}
public class End_Location1
{
public float lat { get; set; }
public float lng { get; set; }
}
public class Polyline
{
public string points { get; set; }
}
public class Start_Location1
{
public float lat { get; set; }
public float lng { get; set; }
}
So you will have the JSON object in your code and through it get the items you want.
Take a look at this link google.github.io/gson/apidocs/com/google/gson/Gson.html
– Gabriel