0

我正在尝试NSJSONSerialization使用下面的代码输出一些数据。

此日志语句有效 NSLog(@"publicData: %@", publicData); ,但是当我尝试获取字典的“描述”时它崩溃了 NSLog(@"data: %@", [dict objectForKey:@"description"]);

我的错误信息:unrecognized selector sent to instance. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance...'

我已经包含了NSLog输出。

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
                                                          URLWithString:@"http://******.com/api/feed"]];

NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSError *jsonParsingError = nil;
NSArray *publicData = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

NSLog(@"data from publicData: %@", publicData);
NSDictionary *dict;

for(int i=0; i<[publicData count];i++)
{
    dict= [publicData objectAtIndex:i];
    NSLog(@"data: %@", [dict objectForKey:@"description"]);

}


publicData: {
data =     (
            {
        address =             {
     address = "street address";
            city = cityname;
            lat = "42.000237034667";
            lng = "12.492805660226";
            province = provincename;
        };
        "category_id" = 20;
        description = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
        id = 1;
        slug = "title-in-en";
        title = "title in En";
    },
            {
        address =             {
            address = "street address";
            city = cityname;
            lat = "42.000237034667";
            lng = "12.492805660226";
            province = provincename;
        };
        "category_id" = 20;
        description = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
        id = 5;
        slug = "title-in-en";
        title = "title in En";
    },

谢谢你的帮助

4

2 回答 2

1

我对 codeplasma 提供的代码进行了一些优化,它看起来像这样:

 NSDictionary *publicData =  [NSJSONSerialization JSONObjectWithData:response
                                                                options:0              
                                                                  error:&jsonParsingError];
    NSArray *objects = [publicData objectForKey:@"data"];
    for(NSDictionary *element in objects) {
       NSLog(@"the description is %@",[element objectForKey:@"description"]);
    } 

您的 publicDataNSDictionary不是NSArray.

于 2013-05-29T19:09:34.590 回答
0

我不知道这是否可行,但你应该尝试用这个替换你的 for 循环:

for(int i=0; i<[[publicData objectForKey:@"data"] count];i++)
{
    dict= [[publicData objectForKey:@"data"] objectAtIndex:i];
    NSLog(@"data: %@", [dict objectForKey:@"description"]);

}
于 2013-05-29T18:56:09.203 回答