Ios GoogleAPI返回复杂的NSDictionary对象
当我解析google API的数据以获取当地警察局的详细信息时,它会返回一个Ios GoogleAPI返回复杂的NSDictionary对象,ios,objective-c,nsdictionary,google-places-api,Ios,Objective C,Nsdictionary,Google Places Api,当我解析google API的数据以获取当地警察局的详细信息时,它会返回一个NSDictionary,其中包含一个复杂的表单: result = ( { geometry = { location = { lat = "22.72187"; lng = "75.8741"; };
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和名称的对象
如何在简单的NSArray
和NSDictionary
中使用此数据
NSDictionary *dict = [NSArray>Object objectatIndex:index];
然后,
dict
将返回键的值,就像它返回[[dict objectforkey:@“name]”;
苹果文档帮助我解决了上述问题:
我使用此代码解决上述词典问题-
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在这里提供了帮助,ArrayMapValues是NSMutablearray类对象。看看我目前正在使用的FTGooglePlacesAPI库。这是一个完整的库,用于使用简单的基于块的Objective-C接口与Google Places API交互
目前没有自述或如何处理,因为我不认为它是最终的/发布的。但是它工作得很好,并且有一个非常详细的、注释良好的示例项目。
你可以在这里找到图书馆:或者您可以利用
NSFoundation
中的一些集合的valueForKeyPath:
方法,该方法经常被忽略,但功能非常强大
例如:
NSDictionary *firstItem = results[0];
NSNumber *lat = [firstItem valueForKeyPath:@"geometry.location.lat"];
在我看来,它更像是一个字典对象数组。无论如何,枚举其中的数组和字典元素应该没有问题,而且由于您没有建议需要哪些数据位,我怀疑您是否会得到非常有用的答案。我已编辑了该问题,请参阅,感谢您的快速回复。