Ios JSON很好地进入了字典,但objectForKey出现了错误

Ios JSON很好地进入了字典,但objectForKey出现了错误,ios,json,rest,ios5,Ios,Json,Rest,Ios5,我有一个提供JSON的RESTful API。我这样称呼它: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. dispatch_async(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL:

我有一个提供JSON的RESTful API。我这样称呼它:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: 
                        kProjectList];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
                               withObject:data waitUntilDone:YES];
    });
}
然后我有了我的fetchedData方法:

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data
    NSError* error;
    NSDictionary* json = [NSJSONSerialization 
                          JSONObjectWithData:responseData //1

                          options:kNilOptions 
                          error:&error];
    //NSArray *projects = [json objectForKey:@"name"]; //2

    NSLog(@"name: %@", json); //3
}
如果我取消注释//NSArray行,我将得到
-[\uu NSCFArray objectForKey:]:发送到实例0x6d81720的无法识别的选择器

注释掉后,我的字典记录:

(
        {
        "created_at" = "2012-04-04T01:46:51Z";
        description = "First Project Created";
        id = 1;
        name = "Test 1";
        "updated_at" = "2012-04-04T01:46:51Z";
    },
        {
        "created_at" = "2012-04-04T01:47:23Z";
        description = "Second Project Created";
        id = 2;
        name = "Test 2";
        "updated_at" = "2012-04-04T01:47:23Z";
    }
)

您有一个字典数组,而不是数组字典。使用objectAtIndex并分配给字典,而不是objectForKey。这样做:

NSDictionary *projects = [[json objectAtIndex:0] objectForKey:@"name"];

我在其他类似的问题中读到,我有NSArray的地方,我应该有NSArray字典。进行此更改会产生完全相同的错误(不同的实例0x########显然)。您的问题是…?谢谢!我必须将NSDictionary*json更改为NSArray,然后使用上面的代码,它工作得非常好。非常感谢。