【问题标题】:JSON Object to NSArrayJSON 对象到 NSArray
【发布时间】:2012-12-20 12:27:02
【问题描述】:

我必须遵循来自服务器的 JSON 响应:

   {
  "theSet": [

  ],
  "Identifikation": 1,
  "Name": "Casper",
  "Adress": "Lovis 23",
  "Location": "At home",
  "Information": "The first, the second and the third",
  "Thumbnail": "none",
 }

我正在像这样检索数据:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"connectionDidFinishLoading");

    NSLog(@"Succeeded! Received %d bytes of data",[data length]);

    NSError *myError = nil;

     NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];

      news = [NSArray arrayWithObjects:[res allKeys], [res allValues], nil];

      NSLog(@"%@", news);

    //[mainTableView reloadData];
}

然后我想将所有 JSON 数据插入到一个数组中,这样我就可以在我的 tableview 中显示数据。

我的tableview代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Adress"];

    return cell;
}

但我的应用程序因错误而崩溃:

-[__NSArrayI objectForKey:]: unrecognized selector sent to instance. 

如何将 JSON 对象插入到 NSArray 中,以便在我的 tableview 中显示它?

【问题讨论】:

    标签: iphone objective-c sdk


    【解决方案1】:

    已编辑:

    我再次查看了您的代码,我之前的回答是错误的。

    在生成 news 时,您将在其中放入 2 个 NSArray 对象。第一个包含所有键,第二个包含 JSON 中的所有值。

    为了在 JSON 中显示每个对象的名称,您应该这样做

    news = [res allKeys];
    jsonResult = res;
    

    // 如果要使用值,请存储您的 json!

    请注意,它们将是无序的。 在您的手机上,您可以:

    NSString *key = [news objectAtIndex:indexPath.row];
    cell.textLabel.text = key;
    
    id object = [jsonResult valueForKey:key];
    cell.detailTextLabel.text = // do something depending on your json type which can have different values
    

    【讨论】:

    • 感谢您的快速回答和有用的信息。我只是想从我的 tableview 中的 JSON 对象中显示名称。您能否指导我正确的方向,我必须写什么而不是 [news objectAtIndex:indexPath.row] 和 objectForKey,才能让它工作?
    • 检查我的编辑,我在阅读您的代码时感到困惑,我之前的答案不正确。
    • 您假设只有一条记录被返回,因此每个键只会有一组值。您也只是显示列表中的所有值。因此该表将仅列出一条记录中的所有值
    • 非常感谢您的回答和帮助,非常感谢!你的两个答案都很好而且很有帮助,但我只能“接受”一个答案:O
    【解决方案2】:

    你的错误

    [__NSArrayI objectForKey:]: unrecognized selector sent to instance.

    告诉你你需要知道的一切。

    这表示NSArray 不理解objectForKey。如果你阅读苹果提供的documentation,你会看到这个。

    你的代码

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Name"];

    期待news NSArray 返回一个响应objectForKey 的对象——很可能是NSDictionary。用于提取 JSON 数据的代码

    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
    news = [NSArray arrayWithObjects:[res allKeys], [res allValues], nil];
    

    只是获取所有字典并将键提取到数组中。

    您需要查看这些代码行 - 这就是您出错的地方。

    查看NSJSONSerialization reference 及其链接的相关示例代码。

    【讨论】:

    • 我在工作中遇到了一些事情,然后忘记发布一些东西。你的回答很含糊不清。将某人转发到文档而不提示他问题出在哪里并不是最佳实践,因为不是每个人都是经验丰富的 objc 程序员,也不是所有人都可以开始消化每一点文档以理解这样的简单错误。
    【解决方案3】:

    我自己找到了一个简单的解决方案,更适合我的项目:

    我刚刚做了以下事情:

        NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&myError];
    
    NSArray news = [NSArray arrayWithObject:res];
    

    这样我就可以使用以下代码在我的 tableview 中显示 JSON 内容。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    
        if(cell == nil){
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
        }
    
    
        cell.textLabel.text = [[news objectAtIndex:indexPath.row] valueForKey:@"name"];
    
        cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"Location"];
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 2015-11-27
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多