如何从NSDictionary中检索值?

时间:2013-09-01 08:07:42

标签: objective-c json nsdictionary

我是新手。 我已经将10个值从网站解析为tableview并链接到详细视图控制器。 如何在每个单元格上点击时显示单个值?

- (void)viewDidLoad
{
    [[self tableView]setDelegate:self];
    [[self tableView]setDataSource:self];
    array = [[NSMutableArray alloc]init];


    [array removeAllObjects];

    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/us/rss/topalbums/limit=10/json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];

    if (connection) {
        webData = [[NSMutableData alloc]init];
    } 



-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
    NSDictionary *feed = [allDataDictionary objectForKey:@"feed"];
    NSArray *arrayOfEntry = [feed objectForKey:@"entry"];

    for (NSDictionary *diction in arrayOfEntry){
        NSDictionary *title = [diction objectForKey:@"title"];
        NSString *label = [title objectForKey:@"label"];

        [array addObject:label];


    }
    [[self tableView] reloadData];

}

1 个答案:

答案 0 :(得分:0)

JSON数据将采用NSMutableArray格式。

NSMutableArray *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];

int i=0;
for (i=0;i<[allDataDictionary count];i++) {
    NSLog("value is %@", [NSString stringWithFormat:@"%@", [[allDataDictionary objectAtIndex:i] objectForKey:@"yourKey"]];)
}