2012-06-10 70 views
1

我無法從谷歌地圖的api結果拉個人地址組件。c#抓取谷歌地圖api address_components

下面是這個網址的結果:

http://maps.googleapis.com/maps/api/geocode/json?address=jobing.com+glendale+arizona&sensor=false

此URL在 「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」]轉換爲Dictionary,表示它是無效轉換。

有沒有人看到我在做什麼錯了?或者,也許有人有一個自定義的對象,我可以讀谷歌地圖地理編碼結果到那個作品?

回答

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