首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析google directions json

解析google directions json
EN

Stack Overflow用户
提问于 2017-08-09 21:16:17
回答 1查看 1K关注 0票数 0

我正在与谷歌方向Api...But工作,我正面临阅读Json response...Does的问题,有人知道这个Api,可以帮助我?我需要获取所有的路线点来绘制我的polyline...which数据是不是很重要?我如何接收和解析它?

这是一个json示例。

https://maps.googleapis.com/maps/api/directions/json?origin=-22.8895625,-47.0714089&destination=-22.892376,-47.027553&key=

我的代码不工作..。

代码语言:javascript
复制
 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;
            }
        }
    }

我真的需要帮助,我已经搜索了,但结果还没有帮助我,这是一个xamarin表单项目,谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-08-10 07:29:43

不要试图提取json的一部分,对整个json进行反序列化,然后从C#对象图中获取所需的数据。

您可以使用json2csharp.com生成C#类结构。

我粘贴了您链接到的json,它生成了以下内容:

代码语言:javascript
复制
public class GeocodedWaypoint
{
    public string geocoder_status { get; set; }
    public string place_id { get; set; }
    public List<string> types { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Bounds
{
    public Northeast northeast { get; set; }
    public Southwest southwest { 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 EndLocation
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class StartLocation
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Distance2
{
    public string text { get; set; }
    public int value { get; set; }
}

public class Duration2
{
    public string text { get; set; }
    public int value { get; set; }
}

public class EndLocation2
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Polyline
{
    public string points { get; set; }
}

public class StartLocation2
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Step
{
    public Distance2 distance { get; set; }
    public Duration2 duration { get; set; }
    public EndLocation2 end_location { get; set; }
    public string html_instructions { get; set; }
    public Polyline polyline { get; set; }
    public StartLocation2 start_location { get; set; }
    public string travel_mode { get; set; }
    public string maneuver { get; set; }
}

public class Leg
{
    public Distance distance { get; set; }
    public Duration duration { get; set; }
    public string end_address { get; set; }
    public EndLocation end_location { get; set; }
    public string start_address { get; set; }
    public StartLocation start_location { get; set; }
    public List<Step> steps { get; set; }
    public List<object> traffic_speed_entry { get; set; }
    public List<object> via_waypoint { get; set; }
}

public class OverviewPolyline
{
    public string points { get; set; }
}

public class Route
{
    public Bounds bounds { get; set; }
    public string copyrights { get; set; }
    public List<Leg> legs { get; set; }
    public OverviewPolyline overview_polyline { get; set; }
    public string summary { get; set; }
    public List<object> warnings { get; set; }
    public List<object> waypoint_order { get; set; }
}

public class RootObject
{
    public List<GeocodedWaypoint> geocoded_waypoints { get; set; }
    public List<Route> routes { get; set; }
    public string status { get; set; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45591795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档