栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C#中的Google Geocoding Json解析问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#中的Google Geocoding Json解析问题

以下是获得所需内容的步骤:

  1. 将您的JSON发布到http://json2csharp.com/中。采取结果类并合并重复项,您将得到:
        public class AddressComponent    {        public string long_name { get; set; }        public string short_name { get; set; }        public List<string> types { get; set; }    }    public class Bounds    {        public Location northeast { get; set; }        public Location southwest { get; set; }    }    public class Location    {        public double lat { get; set; }        public double lng { get; set; }    }    public class Geometry    {        public Bounds bounds { get; set; }        public Location location { get; set; }        public string location_type { get; set; }        public Bounds viewport { get; set; }    }    public class Result    {        public List<AddressComponent> address_components { get; set; }        public string formatted_address { get; set; }        public Geometry geometry { get; set; }        public bool partial_match { get; set; }        public List<string> types { get; set; }    }    public class RootObject    {        public List<Result> results { get; set; }        public string status { get; set; }    }

(您还可以使用JSON作为类粘贴或https://jsonutils.com/来生成初始类型定义。)

  1. 使用Json.NET反序列化JSON,如下所示:

        var root = JsonConvert.DeserializeObject<RootObject>(result);
  2. 您的查询返回了多个结果,因此您需要像这样循环遍历返回的位置:

        foreach (var singleResult in root.results){    var location = singleResult.geometry.location;    var latitude = location.lat;    var longitude = location.lng;    // Do whatever you want with them.}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609215.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号