如何获取json数据并将Array of Array Integer值打印到我的tableview单元格中

时间:2013-10-14 13:11:16

标签: ios objective-c json uitableview

haiii我有json数据,如

{
NetworkMembers =         (
                {
        id = 1;
        memberId = 1;
        networkId = 1;
        position = 0;
        type = H;
    },

最后我像这样放入数组:

for (int i=0; i<[array count]; i++) { 
    NSDictionary *dictionary = [array objectAtIndex:i]; 
    NSArray *array2=[dictionary valueForKey:@"NetworkMembers"]; 
    for (int j=0; j<[array2 count]; j++) { 
        NSDictionary *dictionary2 = [array2 objectAtIndex:j]; 
        NSLog(@"J value %@",[dictionary2 valueForKey:@"type"]); 
        [self.tablArray addObject:[dictionary2valueForKey:@"type"]]; 
        [self.tablArray addObject:[dictionary2 valueForKey:@"id"]]; 
    } 
} 

最后我变得喜欢 表格数组 (     H,     1,     H,     2,     H,     3,     H,     4,     H,     6 然后在索引路径的行的单元格中,我编写了像

这样的代码
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testing"];


if(!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"testing"];

}
cell.textLabel.text = [tablArray objectAtIndex:indexPath.row];
if([tablArray count]>0)
{
           cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row]stringValue];
}

return cell;

但打印细节文字标签我得到的错误就像 因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString stringValue]:无法识别的选择器发送到实例0x109314510' * 第一次抛出调用堆栈:  提前感谢任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

我认为最好使用类型的密钥和id的密钥来获取字典的NSArray,因此tableArray将是:

// Create a property of tableArray to retrieve data later
@property (nonatomic, strong) NSMutableArray *tableArray

// Add data inside your tableArray your viewDidLoad
NSDictionary *dicAllNetworks = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; // Retrieve data from your JSON file inside a NSDictionary

_tableArray = [NSMutableArray array];
for (NSDictionary *dic in [dicAllNetworks objectForKey:@"NetworkMembers"])
{
    [tableArray addObject:@{@"id" : [dic objectForKey:@"id"], @"type": [dic objectForKey:@"type"]}];
}
NSLog(@"%@", tableArray);

你会得到这样的阵列:

tableArray (
        {
        id = 1;
        type = H;
    },
        {
        id = 2;
        type = H;
    }
)

然后修改你的代码:

cell.textLabel.text = [[tablArray objectAtIndex:indexPath.row] objectForKey:@"type"];
if([tablArray count]>0)
{
           cell.detailTextLabel.text=[[tablArray objectAtIndex:indexPath.row] objectForKey:@"id"];
}