简体   繁体   English

在Objective-C中解析JSON

[英]Parsing JSON in Objective-C

I am trying to parse the JSON at this URL: 我试图在此URL解析JSON:

http://maps.googleapis.com/maps/api/geocode/json?address=canada&sensor=false http://maps.googleapis.com/maps/api/geocode/json?address=canada&sensor=false

in Objective-C. 在Objective-C中。 I'm trying to get the northeast latitude under "bounds", and this is the code I've written to try and do that ( feed is an NSDictionary object): 我正在尝试使东北纬度处于“界限”之下,这是我编写的代码,试图做到这一点( feedNSDictionary对象):

NSArray *results = [feed objectForKey:@"results"];
NSDictionary *firstResult = [results objectAtIndex:0];
NSArray *geometry = [firstResult objectForKey:@"geometry"];
NSDictionary *firstGeo = [geometry objectAtIndex:0];
NSArray *bounds = [firstGeo objectForKey:@"bounds"];
NSDictionary *northeast = [bounds objectAtIndex:0];
NSString *neLat = [[NSString alloc] initWithString:[northeast objectForKey:@"lat"]];

Even though it compiles fine and the JSON data is valid, when I run it I get this error: 即使它可以正常编译并且JSON数据有效,但在我运行它时仍会收到此错误:

[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa672390 2012-02-24 01:32:06.321 Geo[570:11603] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa672390' [__NSCFDictionary objectAtIndex:]:无法识别的选择器已发送到实例0xa672390 2012-02-24 01:32:06.321 Geo [570:11603] *由于未捕获的异常'NSInvalidArgumentException'而终止了应用程序,原因:'-[__ NSCFDictionary objectAtIndex:]:无法识别选择器发送到实例0xa672390'

I don't know why this is happening, as I successfully got long_name before… any help is greatly appreciated :) 我不知道为什么会这样,因为我在之前成功获得了long_name ……非常感谢您的帮助:)

Thanks in advance! 提前致谢!

The geometry and bounds are already dictionaries. geometrybounds已经是字典。

     "geometry" : {    // <-- a dictionary!
        "bounds" : {   // <-- also a dictionary!
           "northeast" : {
              "lat" : 83.1150610,
              "lng" : -52.62040200000001
           },

So you should write 所以你应该写

NSDictionary *geometry = [firstResult objectForKey:@"geometry"];
NSDictionary *bounds = [geometry objectForKey:@"bounds"];
NSDictionary *northeast = [bounds objectForKey:@"northeast"];

Check the following statement. 检查以下语句。

NSArray *geometry = [firstResult objectForKey:@"geometry"];

It seems for the geometry , it does not return NSArray. 对于geometry ,似乎不返回NSArray。 It returns NSDictionary 它返回NSDictionary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM