【问题标题】:How do I retrieve values from NSDictionary?如何从 NSDictionary 中检索值?
【发布时间】:2013-09-01 08:07:41
【问题描述】:

我是新手。 我已将网站中的 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];

}

【问题讨论】:

  • “从表中获取每个值”是什么意思?
  • 您要打印键和值吗?
  • 你能把从webservice收到的json输出添加到你的问题中吗?这样帮助你就容易多了……

标签: objective-c json nsdictionary


【解决方案1】:

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"]];)
}

【讨论】:

    猜你喜欢
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多