2013-11-25 55 views
0

當我解析谷歌API的派出所細節的數據,它返回它的NSDictionary一個複雜的形式:谷歌API返回複雜的NSDictionary對象

result =  (
      { 
     geometry =    { 
      location =     { 
       lat = "22.72187"; 
       lng = "75.8741"; 
      }; 
     }; 
     icon = "http://maps.gstatic.com/mapfiles/place_api/icons/police-71.png"; 
     id = 5bf224eb03bee960670c048f7dcdb2684d6aed1f; 
     name = "Name Police Station"; 
     reference = "CoQBgAAAACG6clYx-1ycnenczJfECFggwSCVzxqFR8GKwMYpA1QbP1VTRIgHHYNXW7z0kOw9IRuV9gKJ-ES19tf46CcwUShwT_lznIX36sx_F8aKFjYE3APa0zNWRxSGY0fDQ95HwinR9HXhTWeL0ncPHSLo9cL9FB8OlBJF-tYNRP5ZThuMEhCQH0lSxrelWOd"; 
     type =    (
      police, 
      establishment 
     ); 
     vicinity = "Yeshwant Niwas Road, Sanghi Colony"; 
    }, 

在這裏,我得到了幾何的許多對象, ID,名稱。

如何在一個簡單的NSArrayNSDictionary使用這些數據?

NSDictionary *dict = [NSArray>Object objectatIndex:index]; 

然後dict將按鍵返回值像它會返回[[dict objectforkey:@"name"];

+1

看起來更像字典對象的數組給我。反正它應該是沒有問題列舉其中的陣列和字典元素和你不認爲你想要的數據位,我懷疑你會得到一個非常* *有用的答案。 – trojanfoe

+0

我已編輯的問題,請參見,感謝快速回復。 – Nico

回答

0

蘋果文檔,幫助我解決了上述問題:

我用這個代碼來解決上述字典problem-

NSArray *arryDict = [dictPoliceStatin objectForKey:@"result"]; 

NSEnumerator *enumerator = [arryDict objectEnumerator]; 
NSDictionary *anObject; 

while (anObject = [enumerator nextObject]) { 
    /* code to act on each element as it is returned */ 

    NSMutableDictionary *diceGloable = [[NSMutableDictionary alloc] init]; 



    [diceGloable setObject:[[[anObject objectForKey:@"geometry"]objectForKey:@"location"] objectForKey:@"lat"] forKey:@"lat"]; 
    [diceGloable setObject:[[[anObject objectForKey:@"geometry"]objectForKey:@"location"] objectForKey:@"lng"] forKey:@"lng"]; 
    [diceGloable setObject:[anObject objectForKey:@"name"] forKey:@"name"]; 
    [diceGloable setObject:[anObject objectForKey:@"vicinity"] forKey:@"vicinity"]; 

    [arrayMapValues addObject:diceGloable]; 

} 

ObjectEnumerator幫助這裏,arrayMapsValues是NSMutableArray的類對象。

0

看看我目前正在從事的FTGooglePlacesAPI庫。這是一個使用簡單的,基於塊的Objective-C界面與Google Places API交互的完整庫。

沒有自述或如何,此刻,我不認爲這是最後的/尚未公佈。但它工作得很好,並且有一個非常詳細的,有很好評論的示例項目。

你可以在這裏找到庫:https://github.com/FuerteInternational/FTGooglePlacesAPI


或者你可以從NSFoundation,這往往被忽視,但功能非常強大利用一些藏品valueForKeyPath:方法。

實施例:

NSDictionary *firstItem = results[0]; 
NSNumber *lat = [firstItem valueForKeyPath:@"geometry.location.lat"];