how to read a Json result of google directions API

0

I'm getting this json from the google directions API

link

I need to read it But it's difficult.

What I have so far: (even though awareness 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'm using Newtonsoft and System.Net.Http in this project which is xamarin.forms How do I get only the object that contains the points that I will use in my polyline?

    
asked by anonymous 09.08.2017 / 18:19

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 just picking up the part you will use like this:

HttpWebRequest to get 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 in 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 page of google:

    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.

    
09.08.2017 / 21:36