谷歌地理编码JSON解析问题在C#

编程入门 行业动态 更新时间:2024-10-25 20:30:08
本文介绍了谷歌地理编码JSON解析问题在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我的code工作正常,但我不能似乎能够得到树的深层部位。我想拉的经度和纬度。在code以下拉'状态'没有问题,因为几何OK(在响应的最后)什么是语法。'' - >'的位置? - >纬度和LNG

下面是我的code:

字符串RawAddress =163 Leektown路,新格雷特纳,NJ 08004;串地址= RawAddress.Replace(,+);字符串AddressURL =htt​​p://maps.google/maps/api/geo$c$c/json?address=+地址;VAR的结果=新System.Net.WebClient()DownloadString(AddressURL)。动态数据= JObject.Parse(结果);Lat.Text = data.status;

这是API生成的内容:

{   结果:  {     address_components:[        {           LONG_NAME:山景           SHORT_NAME:山景           类型:本地,政治]        },        {           LONG_NAME:圣克拉拉县           SHORT_NAME:圣克拉拉县           类型:administrative_area_level_2,政治]        },        {           LONG_NAME:加州,           SHORT_NAME:CA,           类型:administrative_area_level_1,政治]        },        {           LONG_NAME:美的,           SHORT_NAME:美国,           类型:国家,政治]        }     ]     的formatted_address:山景城,CA,USA     几何体:{        界限:{           东北:{              纬度:37.4508789,              LNG:-122.0446721           },           西南:{              纬度:37.3567599,              LNG:-122.1178619           }        },        位置 : {           纬度:37.3860517,           LNG:-122.0838511        },        LOCATION_TYPE:近似        视口:{           东北:{              纬度:37.4508789,              LNG:-122.0446721           },           西南:{              纬度:37.3567599,              LNG:-122.1178619           }        }     },     partial_match:真实,     类型:本地,政治]  }  ]  状态:OK  }

解决方案

下面是得到你想要的步骤:

  • 发表在 json2csharp/ 广告JSON。承担由此产生的类和合并重复,你会得到:

    公共类AddressComponent{    公共字符串LONG_NAME {搞定;组; }    公共字符串SHORT_NAME {搞定;组; }    公开名单<串GT;类型{搞定;组; }}公共类边界{    公共场所东北{搞定;组; }    公共场所西南{搞定;组; }}公共类位置{    公共双纬度{搞定;组; }    公共双LNG {搞定;组; }}公共类几何{    公共边界界限{搞定;组; }    公共场所的位置{搞定;组; }    公共字符串LOCATION_TYPE {搞定;组; }    公共边界视{搞定;组; }}公共类结果{    公开名单< AddressComponent> address_components {搞定;组; }    公共字符串的formatted_address {搞定;组; }    公共几何形状{搞定;组; }    公共BOOL partial_match {搞定;组; }    公开名单<串GT;类型{搞定;组; }}公共类RootObject{    公开名单<结果>结果{搞定;组; }    公共字符串状态{搞定;组; }}

  • 使用 Json.NET 像这样反序列化JSON:

    VAR根= JsonConvert.DeserializeObject< RootObject>(结果);

  • 有多种结果的查询返回的,所以你需要遍历返回的位置,像这样:

    的foreach(在root.results VAR singleResult)    {        VAR位置= singleResult.geometry.location;        VAR纬度= location.lat;        VAR经度= location.lng;        //做你想做他们。    }

  • I have my code working fine, but I can't seem to be able to get to the deeper parts of the tree. I'm trying to pull the longitude and the latitude. The code below pulls 'status' no problem as 'OK" (at the very end of the response). What is the syntax for 'geometry' -> 'location' -> 'lat' and 'lng'?

    Here is my code:

    string RawAddress = "163 Leektown Road, New Gretna, NJ 08004"; string Address = RawAddress.Replace(" ", "+"); string AddressURL = "maps.google/maps/api/geocode/json?address=" + Address; var result = new System.Net.WebClient().DownloadString(AddressURL); dynamic data = JObject.Parse(result); Lat.Text = data.status;

    This is what the API generates:

    { "results" : [ { "address_components" : [ { "long_name" : "Mountain View", "short_name" : "Mountain View", "types" : [ "locality", "political" ] }, { "long_name" : "Santa Clara County", "short_name" : "Santa Clara County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "California", "short_name" : "CA", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Mountain View, CA, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 37.4508789, "lng" : -122.0446721 }, "southwest" : { "lat" : 37.3567599, "lng" : -122.1178619 } }, "location" : { "lat" : 37.3860517, "lng" : -122.0838511 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 37.4508789, "lng" : -122.0446721 }, "southwest" : { "lat" : 37.3567599, "lng" : -122.1178619 } } }, "partial_match" : true, "types" : [ "locality", "political" ] } ], "status" : "OK" }

    解决方案

    Here are the steps to get what you want:

  • Post your JSON in json2csharp/. Take the resulting classes and merge duplicates, and you get:

    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; } }

  • Deserialize your JSON with Json.NET like so:

    var root = JsonConvert.DeserializeObject<RootObject>(result);

  • There are multiple results returned for your query, so you need to loop through the locations returned like so:

    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. }

  • 更多推荐

    谷歌地理编码JSON解析问题在C#

    本文发布于:2023-05-29 21:34:05,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/353327.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:地理   JSON

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!