2013-11-25 25 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"];