我无法从谷歌地图的api结果中提取个别地址组件。
以下是从此网址收到的结果:
http://maps.googleapis.com/maps/api/geocode/json?address=jobing.com+glendale+arizona&sensor=false
此网址位于“uri”变量中。这是我用来尝试按类型检索address_component的代码。考虑到我在c#上有点新鲜。
String Output = client.DownloadString(uri);
Dictionary<String, Object> RinkData = JsonConvert.DeserializeObject<Dictionary<String, Object>>(Output);
Dictionary<String, Object> RinkDataResults = (Dictionary<String, Object>) RinkData["results"];
if (RinkDataResults.ContainsKey("formatted_address"))
{
Route = GetAddressComponentByType(RinkDataResults["address_components"], "route");
}
这是我正在使用的函数“GetAddressComponentByType”
protected String GetAddressComponentByType(Object AddressComponents, String AddressType)
{
Boolean MatchFound = false;
String MatchingLongName = "";
Dictionary<String, Object> AddressComponentsDict = (Dictionary<String, Object>)AddressComponents;
foreach (KeyValuePair<String, Object> ac in AddressComponentsDict)
{
Dictionary<String, Object> acDict = (Dictionary<String, Object>) ac.Value;
ArrayList acTypes = (ArrayList) acDict["types"];
foreach (String acType in acTypes)
{
if (acType == AddressType)
{
MatchFound = true;
break;
}
}
if (MatchFound)
{
MatchingLongName = (String) acDict["long_name"];
}
}
return MatchingLongName;
}
问题是,它甚至没有进入我的组件检索功能。它炸弹将RinkData [“results”]转换为一个字典,说它是无效的转换。
有谁看到我做错了什么?或者也许有人有自定义对象我可以阅读谷歌地图地理编码结果有效吗?
答案 0 :(得分:2)
dynamic googleResults = new Uri(uri).GetDynamicJsonObject();
foreach (var result in googleResults.results)
{
foreach (var addressComp in result.address_components)
{
Literal1.Text += "<br />" + addressComp.long_name;
}
}
这是JSON.NET找到here的扩展类。从L.B.回答this question。