Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c JSON和NSMUTABLEARRAY错误_Objective C_Json_Mapkit_Nsjsonserialization - Fatal编程技术网

Objective c JSON和NSMUTABLEARRAY错误

Objective c JSON和NSMUTABLEARRAY错误,objective-c,json,mapkit,nsjsonserialization,Objective C,Json,Mapkit,Nsjsonserialization,我正在尝试检索我正在制作的应用程序的经纬度坐标,但收到以下错误: 2012-02-14 14:00:33.439 MapApp[13750:11c03] { "EVENT_NAME" = "TRIAL EVENT"; lat = "38.671837"; lng = "-77.349533"; } 2012-02-14 14:00:40.759 MapApp[13750:11c03] -[__NSCFArray objectForKey:]: unrecognized

我正在尝试检索我正在制作的应用程序的经纬度坐标,但收到以下错误:

  2012-02-14 14:00:33.439 MapApp[13750:11c03] {
   "EVENT_NAME" = "TRIAL EVENT";
   lat = "38.671837";
    lng = "-77.349533";
 }
2012-02-14 14:00:40.759 MapApp[13750:11c03] -[__NSCFArray objectForKey:]: unrecognized       selector sent to instance 0x7955ea0
 2012-02-14 14:00:40.759 MapApp[13750:11c03] *** Terminating app due to uncaught exception    'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector    sent to instance 0x7955ea0'
  *** First throw call stack:
  (0x1728052 0x18b9d0a 0x1729ced 0x168ef00 0x168ece2 0x2680 0x56764e 0x582b89 0x5829bd    0x580f8a 0x580e2f 0x57effb 0x57f85a 0x568fbf 0x56921b 0x56a0f1 0x4d8fec 0x4de572 0x4d872b 0x2196 0x49f9d6 0x4a08a6 0x4af743 0x4b01f8 0x4a3aa9 0x1e38fa9 0x16fc1c5 0x1661022 0x165f90a   0x165edb4 0x165eccb 0x4a02a7 0x4a1a9b 0x20b8 0x2015 0x1)
  terminate called throwing an exception(gdb)
这是我的代码:

  - (void)viewDidLoad
 {
[super viewDidLoad];

NSString *urlString = [NSString stringWithFormat:jGETUrl];

NSURL *url = [NSURL URLWithString:urlString];

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;

NSMutableArray *datas = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSLog(@"%@", [datas objectAtIndex:0]);


NSNumber *latitude = [datas objectForKey:@"lat"];
NSLog(@"%@", [datas objectForKey:@"lat"]);
NSNumber *longitude =[datas objectForKey:@"lng"]; 
 NSLog(@"%@", [datas objectForKey:@"lng"]);
NSString *eventName =[datas objectForKey:@"EVENT_NAME"];
 NSLog(@"%@", [datas objectForKey:@"EVENT_NAME"]);

CLLocationCoordinate2D coordinate;
coordinate.latitude= latitude.doubleValue;
coordinate.longitude= longitude.doubleValue;
MyLocation *annotation =[[MyLocation alloc] initWithName:eventName address:eventName       coordinate:coordinate];
                         [_mapView addAnnotation:annotation];
 }
我不熟悉JSON和解析:我还尝试了以下组合

  - (void)viewDidLoad
 {
[super viewDidLoad];

NSString *urlString = [NSString stringWithFormat:jGETUrl];

NSURL *url = [NSURL URLWithString:urlString];

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;

NSMutableArray *datas = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSLog(@"%@", [datas objectAtIndex:0]);


NSNumber *latitude = [datas objectAtIndex:1];
NSLog(@"%@", [datas objectAtIndex:1]);
NSNumber *longitude =[datas objectAtIndex:2]; 
 NSLog(@"%@", [datas objectAtIndex:2]);
NSString *eventName =[datas objectAtIndex:3];
 NSLog(@"%@", [datas objectAtIndex:3]);

CLLocationCoordinate2D coordinate;
coordinate.latitude= latitude.doubleValue;
coordinate.longitude= longitude.doubleValue;
MyLocation *annotation =[[MyLocation alloc] initWithName:eventName address:eventName       coordinate:coordinate];
                         [_mapView addAnnotation:annotation];
 }
它返回每个索引的事件名称、lat、lng。请帮助..

这是您的代码:

NSLog(@"%@",              [datas objectAtIndex:        0]);
NSNumber *latitude =      [datas objectForKey:         @"lat"];
                          ^^^^^^^^^^^^^^^^^^^^^
您必须决定
数据是数组(您可以使用
objectAtIndex:
)还是字典(您可以使用
objectForKey:
)。不能同时使用两者,因为对象不能同时是
NSArray
NSDictionary

从异常消息(
-[\uu NSCFArray objectForKey:]:无法识别的选择器
)中,您可以看到
数据
对象实际上是一个数组,您不能在其上使用
objectForKey:

你应该试试这样的方法:

NSDictionary *dict = [datas objectAtIndex:0];
NSNumber *latitude = [dict objectForKey:@"lat"];
这是您的代码:

NSLog(@"%@",              [datas objectAtIndex:        0]);
NSNumber *latitude =      [datas objectForKey:         @"lat"];
                          ^^^^^^^^^^^^^^^^^^^^^
您必须决定
数据是数组(您可以使用
objectAtIndex:
)还是字典(您可以使用
objectForKey:
)。不能同时使用两者,因为对象不能同时是
NSArray
NSDictionary

从异常消息(
-[\uu NSCFArray objectForKey:]:无法识别的选择器
)中,您可以看到
数据
对象实际上是一个数组,您不能在其上使用
objectForKey:

你应该试试这样的方法:

NSDictionary *dict = [datas objectAtIndex:0];
NSNumber *latitude = [dict objectForKey:@"lat"];

很好的解释。非常感谢。最后我自己解决了这个问题,忘了检查答案,不过还是要谢谢你!很好的解释。非常感谢。最后我自己解决了这个问题,忘了检查答案,不过还是要谢谢你!