iamdlm
iamdlm

Reputation: 1973

Deserialize Google Directions JSON

First time working with JSON and related, I'm trying to get distance/duration of all possible routes from this request: https://maps.googleapis.com/maps/api/directions/json?origin=41.2091585,-8.5763016&destination=41.258913,-8.636942&mode=driving&alternatives=true&avoid=tolls&language=pt-PT&key=AIzaSyDuhdvLAny3MpraXKX-bahkXZJolm7KLbE

I got the following classes using "Paste JSON as Classes" and create one Class for each of the following ones:

public class Rootobject
    {
        public Geocoded_Waypoints[] geocoded_waypoints { get; set; }
        public Route[] routes { get; set; }
        public string status { 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 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 Via_Waypoint[] 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; }
}

For the query mentioned above I have 3 different routes (aka "legs") and I want to get the distance/duration of each one.
I came up with the following but it's not working.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(query);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();

    if (!string.IsNullOrEmpty(result))
    {
        Distance t = JsonConvert.DeserializeObject<Distance>(result);
        string distance1_Value = t.value;
        string distance1_Text = t.text;

        Duration d = JsonConvert.DeserializeObject<Duration>(result);
        string duration1_Value = d.value;
        string duration1_Value = d.text;
    }
}

Any help?
PS: If anyone can show me how to iterate throw each "legs" that would be great.

EDIT: Forgot to mention I'm using Newtonsoft.

Upvotes: 0

Views: 1379

Answers (1)

Arilo Souza
Arilo Souza

Reputation: 11

I found the solution a few days ago...

First, i do the same of you, but still not working for me, so i take a look deep inside the classes auto generated by this tool and notice that some classes was missing the 's' at the end of its names.. Step, where correct name 'Steps' Leg. correct name 'Legs' Route. correct name 'Routes'

Do you have to fix this at entire document. After this, you can convert directly...

Take a look at correct answer: [1]: http://pastie.org/10935748#9

Upvotes: 1

Related Questions