The*_*Guy 0 json objective-c nsdictionary google-geocoder
我正在制作一个iPhone应用程序,我从http://maps.googleapis.com/maps/api/geocode/json?address=canada&sensor=true中检索JSON数据,然后将其存储在NSDictionary对象中.我的代码编译得很好,JSON数据有效.但是,当我查找某些键时,即使键存在也返回nil并且我没有输入错误.这是唯一有效的键(feed是一个NSDictionary对象):
NSString *status = [[NSString alloc] initWithFormat:[feed objectForKey:@"status"]];
Run Code Online (Sandbox Code Playgroud)
但是当我做这样的事情时:
NSString *longName = [[NSString alloc] initWithFormat:[feed objectForKey:@"long_name"]];
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
WebKit在webView中丢弃了一个未捕获的异常:shouldInsertText:replacementDOMRange:givenAction:delegate: * - [NSPlaceholderString initWithFormat:locale:arguments:]:nil参数
我有两个问题:我该如何解决这个问题?此外,由于如果存在多个具有相同名称的位置(即"Springfield"),此Google Geocoder可以返回多个位置,如何选择我想要引用的键?(比如"long_name","lat"等...)
提前致谢!
"状态"工作的原因是因为它位于字典的根部,而长名称和大多数其他部分位于子数组中的子字典中.尝试这样的事情:
NSArray *results = [feed objectForKey:@"results"];
NSDictionary *firstResult = [results objectAtIndex:0];
NSArray *addressComponents = [firstResult objectForKey:@"address_components"];
NSDictionary *firstAddress = [addressComponents objectAtIndex:0];
NSString *longName = [firstAddress objectForKey:@"long_name"];
Run Code Online (Sandbox Code Playgroud)
如果你想获得所有的值,你当然需要设置循环 - 我给出的例子显示了如何从返回的第一个结果中的第一个地址获取长名称.
| 归档时间: |
|
| 查看次数: |
6249 次 |
| 最近记录: |