【问题标题】:Json SBJSON error - Confused about NSDictionaryJson SBJSON 错误 - 对 NSDictionary 感到困惑
【发布时间】:2013-01-06 11:25:40
【问题描述】:

您好,我是编程新手。

我对 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;

}

【问题讨论】:

  • 您的问题到底是什么? NSDictionary 有什么问题?您期待不同的价值吗?
  • 你提取代码中的字符串部分 //NSString * content = [results objectForKey:@"n_name"];内容 = [结果 objectForKey:@"n_name"];而在 cell.textLabel.text = [content objectAtIndex:indexPath.row];你把它当作一个数组。你能解释一下为什么字符串“内容”对象和数组一样吗?

标签: iphone json dictionary


【解决方案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];

【讨论】:

  • 您能否提供问题的详细信息,以便我可以解决该问题。
  • 我无法将数据库值放入我的表中。我认为错误可能是 - cell.textLabel.text = [studName objectAtIndex:indexPath.row];我确实获得了数据库值,但我无法将它们放入单元格中。
  • 在将 [studName objectAtIndex:indexPath.row] 的值分配给 cell.textLabel.text 之前尝试 NSLog。检查它是否给出了价值
  • 我收到这条消息:在抛出“NSException”实例后调用终止。我想我做错了 NSMutableArray。你能不能再检查一下我关于 NSMutableArray 的代码。在编程方面,我真的很陌生。再次感谢所有帮助
  • 你是否在表委托方法中使用了 numberOfRowsInTable 如果没有,那么也使用它。在 numberOf Sections 方法之后写这个: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { [studName count]; }
猜你喜欢
  • 2013-06-02
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 2012-07-22
  • 2013-05-13
相关资源
最近更新 更多