【问题标题】:how to load from JSON and parse into tableView on iOS 5如何从 JSON 加载并解析到 iOS 5 上的 tableView
【发布时间】:2012-12-21 23:21:14
【问题描述】:

我有一个播放一些链接的音频播放器,我希望通过 JSON 将链接加载到 tableView 中。

这是我的 tableView,它显示了链接:(注释掉了,但没有 json 也可以工作)

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    int row = indexPath.row;

    [[cell textLabel] setText:[_radioNames objectAtIndex:row]];
    [[cell detailTextLabel] setText:[_radioSubtitles objectAtIndex:row]];
    if(row == _currentRadio) {
        [cell setAccessoryView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"now_playing.png"]] autorelease]];
    } else {
        [cell setAccessoryView:nil];
    }


    return cell;
}
 */

这是我获取 JSON 文件的地方:

// Download JSON
    NSString *jsonString = [NSString
                            stringWithContentsOfURL:[NSURL URLWithString:@"http://www.xxxxxxxxx/radioliste.json"]
                            encoding:NSStringEncodingConversionAllowLossy
                            error:nil];
    // Create parser
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];
    [parser release], parser = nil;
    // Set tableData
    [self setTableData:[results objectForKey:@"items"]];

    NSLog(jsonString); // THIS IS SHOWING ME THE WHOLE JSON FILE, DON'T KNOW IF THAT OK?

这是 JSON 数据列表:(或者我认为)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // Change UITableViewCellStyle
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleSubtitle
                 reuseIdentifier:CellIdentifier] autorelease];
    }

    // Get item from tableData
    NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
    // Set text on textLabel
    [[cell textLabel] setText:[item objectForKey:@"title"]];
    // Set text on detailTextLabel
    [[cell detailTextLabel] setText:[item objectForKey:@"description"]];

    return cell;
}

这是我的 JSON 文件:

{
    "radioliste": {
    "items": [
        {
            "id": "The Voice",
            "string": "Afspil The Voice",
            "string": "LINK"
        }
    ]
    }
}

所以这是我的问题...如何从 JSON 文件正确加载流媒体链接并将它们解析到 tableView 中? JSON 文件是否有效?

【问题讨论】:

  • 不,JSON 无效,因为它具有相同对象的两倍键“字符串”。来自json.org一个对象是一组无序的名称/值对。(而无序意味着你无法区分哪个“字符串”是链接之一)

标签: ios json parsing ios5 tableview


【解决方案1】:

首先,不要使用 initWithContentsOfURL... 这是一个阻塞调用,会冻结您的应用。使用 NSURLConnection 异步方式从网络获取数据。

其次,NSJSONSerialization 从 iOS 5 开始就可用。使用它。

所以,NSURLConnection 会建立一个 NSData。 NSJSONSerialization JSONObjectWithData: 将把 NSData 变成一个对象。在您的情况下,它将是一个 NSDictionary。

您粘贴的 JSON 文件与您在 cellForRowAtIndexPath 中使用的对象键名不匹配。我不确定会发生什么,因为第一项中的两个键是“字符串”。但是假设你的意思是标题和描述,你可以使用类似的东西:

cell.textLabel.text = tableData[@"items"][indexPath.row][@"title"];

将标题放入表格单元格中。

【讨论】:

  • 好的,我如何使用我已经安装的 NSJSONSerialization?我应该用 NSURLConnection 替换 initWithContentsOfURL 吗?我知道这可能是我的愚蠢问题,但我不知道如何使用 JSON
【解决方案2】:

像这样使用原生 JSON 库怎么样:

NSData* data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];

NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:(__bridge NSData *)data options:NSJSONReadingMutableContainers error:nil];

而不是 SBJSON?

编辑:刚刚注意到您的 JSON 存在一些问题。您在字典中有重复键:字符串。

【讨论】:

  • 你不要使用额外的库,因为现在有一个 JSON 的本机接口。
  • 好的,我该如何使用它?
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2012-07-02
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多