Json SBJSON错误 - 对NSDictionary感到困惑

时间:2013-01-06 11:25:41

标签: iphone json dictionary

嗨,我是编程新手。

我在NSDictionary和Table单元格方面存在一些问题。我试图找到好的教程,但没有找到任何可以帮助我的东西。我正在使用JSONKit来解析json。我有一个旧版本的xcode,只有模拟器4.3。

我想做的是将我的Json解析成带有单元格的表格。

我要解析的json:

{[{"n_id":"1","name":"Green","age":"23"}]}

我的主要问题。 我没有得到任何错误,但当我运行应用程序时,它只是自动关闭。 当我注释掉//cell.textLabel.text = [studName objectAtIndex:indexPath.row];并用cell.textLabel.text = @“Test”替换它;应用程序和输出工作正常。 我也能得到NSLog();值

我认为我的错误在cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 提前致谢

// Json2ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];



NSData * JSONdata =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://myurl.php"]];

 if([JSONdata length] == 0){
 [JSONdata release];
 return;
 }

NSArray *students =[JSONdata objectFromJSONData];

studName = [[NSMutableArray alloc] init];

for(NSDictionary *student in students)
{
    NSLog(@"Name: %@", [student objectForKey:@"name"]);
    NSLog(@"Age: %@", [student objectForKey:@"age"]);

    content = [student objectForKey:@"name"];
    if(content)
       [studName addObject:content];

}

}


 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

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

// Configure the cell...
cell.textLabel.text = [studName objectAtIndex:indexPath.row];
return cell;

}

我不确定NSMutableArray。

// Json2ViewController.h

@interface Json2ViewController : UIViewController {
NSArray * list;
NSString * content;
NSMutableArray *studName;

}

1 个答案:

答案 0 :(得分:1)

试试这个:

 //Declare NSMutableArray *studName= [[NSMutableArray alloc] init]; in your .h file.
// 3. iterate the array; each element is a dictionary...


 for (NSDictionary *results in list)
    {
        // 3 ...that contains a string for the key "stunde"
        content = [results objectForKey:@"n_name"];
        if(content)
            [studName addObject:content];
    }  

并在表委托方法单元格中用于索引

的行
  // Configure the cell...
            cell.textLabel.text = [studName objectAtIndex:indexPath.row];