简体   繁体   English

Json SBJSON错误-对NSDictionary感到困惑

[英]Json SBJSON error - Confused about NSDictionary

Hi I am new to programming. 嗨,我是编程新手。

I have some issues with NSDictionary and Table cells. 我的NSDictionary和Table单元格存在一些问题。 I Tried to find good tutorials, but did not find anything that could help me. 我试图找到好的教程,但没有找到任何可以帮助我的东西。 I am using JSONKit to parse json. 我正在使用JSONKit解析json。 I have an older version of xcode and only have simulator 4.3. 我有一个较旧的xcode版本,并且只有模拟器4.3。

What i would like to do is to parse my Json into a table with cells. 我想做的是将我的Json解析为带有单元格的表。

The json I want to parse: 我想解析的json:

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

My main problem. 我的主要问题。 I dont get any errors, but when i run the app it just close automatically. 我没有收到任何错误,但是当我运行该应用程序时,它只会自动关闭。 When i comment out //cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 当我注释掉//cell.textLabel.text = [studName objectAtIndex:indexPath.row]; and replace it with just cell.textLabel.text = @"Test"; 并将其替换为cell.textLabel.text = @“ Test”; The app and the output works fine. 该应用程序和输出工作正常。 I am also able to get the NSLog(); 我也可以得到NSLog(); values 价值观

I think my error is in the cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 我认为我的错误是在cell.textLabel.text = [studName objectAtIndex:indexPath.row];中。 Thanks in advance 提前致谢

//Json2ViewController.m //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;

} }

I am not sure about NSMutableArray. 我不确定NSMutableArray。

//Json2ViewController.h //Json2ViewController.h

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

} }

Try this: 尝试这个:

 //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];
    }  

and in table delegate method cell for row at index 在表委托方法的单元格中索引的行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM