当前位置: 首页 > 编程语言 > C#

使用Json.Net解析google map geocode json对对象的响应分享

时间:2023-04-10 20:11:04 C#

C#学习教程:使用Json.Net解析googlemapgeocodejson对对象的响应所以我想遍历它们并使用GoogleGeocode更新我的数据库。我坚持如何解析JSOn结果以获得我需要的东西:varaddress="http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+查看,+CA&sensor=false";varresult=newSystem.Net.WebClient().DownloadString(地址);GoogleGeoCodeResponse测试=JsonConvert.DeserializeObject(结果);我想我可以简单地构建一个swift类并使用JSON.Net来反向序列化结果,它有点工作,但我想我在我的类结构上搞砸了:publicclassGoogleGeoCodeResponse{publicstringstatus{get;放;}公共几何几何{得到;放;}}publicclassgeometry{publicstringlocation_type{get;放;}公共位置位置{get;放;}}publicclasslocation{publicstringlat{get;set;}publicstringlng{get;set;}}以下是Google返回的示例:{"status":"OK","results":[{“类型”:[“街道地址”],“格式化地址”:“1600AmphitheatrePkwy,山景城,CA94043,美国”,“地址组件”:[{“long_name”:“1600”,“short_name”:“1600”,"types":["street_number"]},{"long_name":"AmphitheatrePkwy","short_name":"AmphitheatrePkwy","types":["route"]},{"long_name":"MountainView","short_name":"MountainView","types":["locality","political"]},{“long_name”:“加利福尼亚”,“short_name”:“CA”,“类型”:[“administrative_area_level_1”,“政治”]},{“long_name”:“美国”,“short_name”:“US","types":["country","political"]},{"long_name":"94043","short_name":"94043","types":["postal_code"]}],"geometry":{“位置”:{“lat”:37.4219720,“lng”:-122.0841430},“location_type”:“ROOFTOP”,“viewport”:{“西南”:{“lat”:37.4188244,“lng”:-122.0872906},"northeast":{"lat":37.4251196,"lng":-122.0809954}}}}]}我在这里遗漏了一些简单的东西,我知道,任何人?我试过这个并做了一个简单的测试并且它有效(添加结果和其他):publicclassGoogleGeoCodeResponse{publicstringstatus{get;set;}publicresults[]results{get;set;}}publicclassresults{publicstringformatted_address{get;set;}publicgeometrygeometry{get;放;}公共字符串}publicaddress_component[]address_components{get;放;}}publicclassgeometry{publicstringlocation_type{get;放;}公共位置位置{get;放;}}publicclasslocation{publicstringlat{get;放;}publicstringlng{get;放;}}publicclassaddress_component{publicstringlong_name{get;放;}publicstringshort_name{get;放;}publicstring[]types{get;放;您可以使用动态对象而不是定义对象publicstaticdynamicGEOCodeAddress(StringAddress){varaddress=String.Format("http://maps.google.com/maps/api/geocode/json?address={0}&sensor=false",Address.Replace("","+"));varresult=newSystem.Net.WebClient().DownloadString(地址);JavaScriptSerializerjss=newJavaScriptSerializer();返回jss.Deserialize(result);我参考GoogleGeoKitC#对象代码做了类似的事情我添加了一些额外的类,不确定它们是否是新的API但认为这可能对某人有所帮助。公共类GoogleGeoCodeResponse{公共结果[]结果{得到;放;}公共字符串状态{得到;放;}}publicclassresults{publicaddress_component[]address_components{get;放;}publicstringformatted_address{得到;放;}公共几何几何{得到;放;}publicstring[]types{get;放;}}publicclassaddress_component{Stringlong_name{get;放;}Stringshort_name{得到;放;}字符串类型{get;放;}}publicclassgeometry{publicboundsbounds{get;放;}公共位置位置{get;放;}publicstringlocation_type{得到;放;}公共视口视口{get;放;}}publicclasslocation{publicstringlat{get;放;}publicstringlng{get;放;}}publicclassviewport{publicnortheastnortheast{get;放;}publicsouthwest西南{get;放;}}publicclassbounds{publicnortheastnortheast{get;放;}}publicclassnortheast{publicstringlat{get;放;}公共字符串}}publicclasssouthwest{publicstringlat{get;放;}publicstringlng{get;放;感谢上面的JEuvin,我能够轻松地从XML切换到Json,并且对上面的代码进行了一些修改(特别是将lat和lng更改为decimal或double),还必须将address_components.types更改为string[]到让它为我工作然后我重构了一点,以便可以互换地从XML或Json中反序列化相同的类。也许这也会帮助别人……usingSystem;使用System.Xml.Serialization;[可序列化][XmlType(AnonymousType=true)][XmlRoot(Namespace="",IsNullable=false)]publicclassGeocodeResponse{publicGeocodeResponse(){}[XmlElement("result")]publicresults[]results{get;放;}公共字符串状态{得到;放;}}[XmlType(AnonymousType=true)]publicclassresults{publicresults(){}[XmlElement("address_component")]publicaddress_component[]address_components{get;放;}publicstringformatted_address{得到;放;}公共几何几何{得到;放;}[XmlElement("type")]publicstring[]types{get;放;}publicstring[]postcode_localities{get;放;}publicboolpartial_match{得到;放;}publicstringplace_id{得到;放;}}[XmlType(AnonymousType=true)]publicclassaddress_component{publicaddress_component(){}publicstringlong_name{get;放;}publicstringshort_name{get;放;}[XmlElement("类型")]公共字符串[]类型{得到;放;}}[XmlType(AnonymousType=true)]publicclassgeometry{publicgeometry(){}publicboundsbounds{get;放;}公共位置位置{get;放;}publicstringlocation_type{得到;放;}公共视口视口{get;放;}}[XmlType(AnonymousType=true)]publicclasslocation{publiclocation(){}publicdoublelat{get;放;}publicdoublelng{get;放;}}[XmlType(AnonymousType=true)]publicclassviewport{publicviewport(){}publicnortheastnortheast{get;放;}publicsouthwest西南{get;放;}}[XmlType(AnonymousType=true)]publicclassbounds{publicbounds(){}publicnortheastnortheast{get;放;}}[XmlType(AnonymousType=true)]publicclassnortheast{publicnortheast(){}publicdoublelat{get;放;}publicdoublelng{get;放;}}[XmlType(AnonymousType=true)]publicclasssouthwest{publicsouthwest(){}publicdoublelat{get;放;}民众双液化天然气{得到;放;(通过添加postcode_localities和partial_match属性进行编辑)确保类是Serializable并允许nullables以上就是C#学习教程的全部内容:使用Json.Net解析googlemapgeocodejson对对象的响应,如果对你有用,需要多了解C#学习教程,希望大家多多关注——[Serializable][XmlType(AnonymousType=true)][XmlRoot(Namespace="",IsNullable=true)]publicclassGeocodeResponse{publicGeocodeResponse(){//可以为空,也可以在此处启动属性}[XmlElement("location")][Display(Name="location")]//添加json属性以及publiclocationlocation{get;放;}公共字符串状态{得到;放;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。