1

你好我得到一个看起来像这样的json:

  features: (
    {
    attributes =         {
        Gecontroleerd = Ja;
    };
    geometry =         {
        x = "5.968097965285907";
        y = "52.50707112779077";
    };
}
)

从这段代码:

NSDictionary *root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray *data = [root objectForKey:@"features"];
NSLog(@"features: %@", data );

for (NSArray *row in data) {
    NSString *latitude = row[5];
    NSString *longitude = row[7];
    NSString *crimeDescription = @"test";
    NSString *address = @"banaan";

你需要 x 值 5.968097965285907 纬度和 y 值 52.50707112779077 经度

但我得到这个错误:

 [__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance   0x14831450
 2012-11-14 10:10:59.000 ArrestPlotter[6330:c07] *** Terminating app due to uncaught  exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x14831450'
 *** First throw call stack:
(0x1860012 0x1655e7e 0x18eb4bd 0x184fbbc 0x184f94e 0x2dbc 0x3b8f 0x1cc1e 0x16696b0 0xfa0035 0x17e3f3f 0x17e396f 0x1806734 0x1805f44 0x1805e1b 0x224a7e3 0x224a668 0x4a765c 0x25bd 0x24e5 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

我需要选择哪一行?

我想唯一的事情是需要更改行号。或者也许应该有这样的东西:[1][5]。我不太确定这是如何工作的

4

2 回答 2

3
NSArray *data = [root objectForKey:@"features"];
NSLog(@"features: %@", data );

for (NSDictionary *dic in data) {
  NSDictionary geometry = [dic objectForKey:@"geometry"];
  // Do what you want..
  NSString *myAwesomeX = [geometry objectForKey:@"x"];
  NSString *myAwesomeY = [geometry objectForKey:@"y"];
}
于 2012-11-14T09:23:51.473 回答
2

这里的问题是您试图将选择器消息发送到对象行,即内存中的NSDictionary (NSCFDictionary?) 对象,并且您正试图像NSArray一样管理它。

该方法objectAtIndexedSubscript(由 row[5] 和 row[7] 调用)存在于 NSDictionary 中,但不存在于 NSArray 中。

改变

for (NSArray *row in data) {

经过

for (NSDictionary *row in data) {

此外,您必须更改内部数据的管理,查看您的日志语句的结果并按照它进行操作。

我希望这个能帮上忙!

于 2012-11-14T09:30:41.980 回答