how to read a result Json from google Directions API

Asked

Viewed 633 times

0

I’m getting this json from google Directions API

https://maps.googleapis.com/maps/apidirections/json?origin=-22.8895625,-47.0714089&Destination=-22.892376,-47.027553&key=

I need to read it But it’s hard.

What I have so far: (even though I am aware that it is wrong)

 public static async Task<List<Model.Localizacao>> GetDirectionsAsync(Localizacao locUser, Localizacao locLoja)
    {
        using (var client = new HttpClient())
        {
            try
            {
                List<Model.Localizacao> lstLoc = new List<Model.Localizacao>();
                var json = await client.GetStringAsync("https://maps.googleapis.com/maps/api/directions/json?origin=" + locUser.latitude + "," + locUser.longitude + "&destination="+ locLoja.latitude+","+locLoja.longitude+"&key=" + GOOGLEMAPSKEY);
                json = json.Substring(json.IndexOf('['));
                json = json.Substring(0, json.LastIndexOf(']') + 1);
                lstLoc = JsonConvert.DeserializeObject<List<Model.Localizacao>>(json);
                return lstLoc;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return null;
            }
        }
    }

my class

namespace neoFly_Montana.Model
{
class Localizacao
{
    public double latitude { get; set; }
    public double longitude { get; set; }
}
}

I am using Newtonsoft and System.Net.Http in this project which is Xamarin.Forms How do I receive only the object containing the points I will use in my Polyline?

  • Take a look at this link google.github.io/gson/apidocs/com/google/gson/Gson.html

1 answer

0


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.

  • If this works, I owe you my life

  • Yes, I owe you life hahah Thank you very much

  • kkkkkkk You’re welcome

Browser other questions tagged

You are not signed in. Login or sign up in order to post.