0

Iphone 如何从这个哈希中获取 ID 和所有者到 NString 中。

  { "photos": { "page": 1, "pages": 26, "perpage": 10, "total": "259", 
        "photo": [
          { "id": "8493465829", "owner": "77009896@N08", "secret": "e0a8381648", "server": "8367", "farm": 9, "title": "IMG_9444", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8494565062", "owner": "77009896@N08", "secret": "6aaa0f41b9", "server": "8372", "farm": 9, "title": "IMG_9443", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493492761", "owner": "77009896@N08", "secret": "30f4da89b4", "server": "8523", "farm": 9, "title": "IMG_9488", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493492007", "owner": "77009896@N08", "secret": "95c0f2215e", "server": "8529", "farm": 9, "title": "IMG_9487", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493471375", "owner": "77009896@N08", "secret": "812fddc8cd", "server": "8516", "farm": 9, "title": "IMG_9453", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493463059", "owner": "77009896@N08", "secret": "7eedfd6b7c", "server": "8389", "farm": 9, "title": "IMG_9440", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8494572044", "owner": "77009896@N08", "secret": "e4d514021d", "server": "8087", "farm": 9, "title": "IMG_9454", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493462429", "owner": "77009896@N08", "secret": "43822f14a5", "server": "8526", "farm": 9, "title": "IMG_9439", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493489419", "owner": "77009896@N08", "secret": "8b50906ebe", "server": "8515", "farm": 9, "title": "IMG_9480", "ispublic": 1, "isfriend": 0, "isfamily": 0 },
          { "id": "8493488459", "owner": "77009896@N08", "secret": "7ec696b8d0", "server": "8242", "farm": 9, "title": "IMG_9479", "ispublic": 1, "isfriend": 0, "isfamily": 0 }
        ] }, "stat": "ok" }

这段代码给了我一个错误

NSDictionary *photos =[JSON objectForKey:@"photos"];
NSDictionary *picturesOfLocations = photos[@"photo"];

NSString *flickUserID = picturesOfLocations[@"id"];

// NSURL *url = [[NSURL alloc] initWithString:[picturesOfLocations objectForKey:@"photo"]];
//NSData *data = [NSData dataWithContentsOfURL:url];
// cell.imageView.image = [[UIImage alloc] initWithData:data];
NSLog(@"%@", flickUserID);

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKeyedSubscript:]: unrecognized selector sent to instance 0x7527430' *** First throw call stack: (0x1ce3012 0x1120e7e 0x1d6e4bd 0x1cd2bbc 0x1cd294e 0x24dd9 0x171f2 0x185b9 0x4a4753f 0x4a59014 0x4a497d5 0x1c89af5 0x1c88f44 0x1c88e1b 0x1c3d7e3 0x1c3d668 0x64ffc 0x244d 0x2375) libc++abi.dylib: terminate called throwing an exception

尝试以下操作时出现同样的错误

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {



NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:JSON options:kNilOptions error:nil];
NSString* stringPhoho = [[[[dict valueForKey:@"photos"] valueForKey:@"photo"] objectForKey:0] valueForKey:@"owner"];
NSLog(@"%@",stringPhoho);
4

5 回答 5

2
NSDictionary *picturesOfLocations = photos[@"photo"];

是错误的,因为它是一个 NSArray ( [ ) 所以:

NSArray *picturesOfLocations = photos[@"photo"];

最后你有一个 NSDictionary 的 NSArray,你可以使用:

//parse each photo
for (NSDictionary * photo in picturesOfLocations ){
     NSString* flickUserID = [photo  objectForKey:@"id"];
     NSLog(@"%@", flickUserID);
    //etc...
}
于 2013-02-21T10:23:08.023 回答
0

请尝试以下语法

使用 JSOn 序列化如下

NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

并解析owner如下

NSString* stringPhoho = [[[[dictResponse valueForKey:@"photos"] valueForKey:@"photo"] objectForKey:0] valueForKey:@"owner"];

它会给你owner 77009896@N08位于index 0数组上的photo

您可以根据您的要求访问所有“所有者”ID,即使用 for 循环,如果您使用的是 tableview,则使用didSelectRow方法,使用按钮标签等...

于 2013-02-21T10:05:43.650 回答
0

photos[@"photo"] 给出了我认为的错误....

它应该是这样的

... = [照片objectForKey:@“照片”];

请参阅 NSDictionary 的类参考!

于 2013-02-21T10:06:54.807 回答
0

'photo' 是字典数组,而不是字典。

于 2013-02-21T10:08:01.227 回答
0

“照片”标签是一个字典数组。我还没有尝试过,但认为这应该适合你。

NSDictionary *photos =[JSON objectForKey:@"photos"];
NSArray *picturesOfLocations = [photos objectForKey:@"photo"];

for(NSDictionary* pic in  picturesOfLocations )
{
   NSString *flickUserID = [pic objectForKey:@"id"] 
}
于 2013-02-21T10:18:03.707 回答