Iphone 在tableview中解析字典的JSON数组

Iphone 在tableview中解析字典的JSON数组,iphone,ios,json,nsarray,nsdictionary,Iphone,Ios,Json,Nsarray,Nsdictionary,我被困在解析dict的JSON数组的点上http://www.cjs-design.nl/json.php 进入桌面视图。我只想先显示标题,稍后我会找出详细视图 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCe

我被困在解析dict的JSON数组的点上http://www.cjs-design.nl/json.php 进入桌面视图。我只想先显示标题,稍后我会找出详细视图

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


NSString *rawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.cjs-design.nl/json.php"]];

// No connection or file not found
if ([rawJson length] == 0) {
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Foutmelding" message:@"De URL kon niet worden gevonden" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
    [rawJson release];
    return;
}


SBJSON *parser = [[[SBJSON alloc] init] autorelease];
// 1. get the top level value as a dictionary
NSDictionary *jsonObject = [parser objectWithString:rawJson error:NULL];
// 2. get the object as an array
NSArray *list = [jsonObject objectForKey:@"book"];
// 3. iterate the array; each element is a dictionary.    

for (NSDictionary *book in list)
{
    // that contains a string for the key "title"
    NSString *title = [book objectForKey:@"title"];
    cell.textLabel.text = title;
}
return cell;

}

在viewDidLoad这样的地方,您需要解析JSON。然后实现UITableView的数据源方法

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [list count];
}

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

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }
    NSDictionary *book = [list objectAtIndex:indexPath.row];
    NSString *title = [book objectForKey:@"title"]; 

    cell.titleLabel.text =  title;
}
当我记录它们时,它们都打印Null,所以字符串不会被解析?该字符串包含数组tho的字典

- (void)viewDidLoad
{
    [super viewDidLoad]; 
    NSString *rawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.cjs-design.nl/json.php"]];
    // No connection or file not found
    if ([rawJson length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Foutmelding" message:@"De URL kon niet worden gevonden" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        [rawJson release];
        return;
    }
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    // 1. get the top level value as a dictionary
    NSDictionary *jsonObject = [parser objectWithString:rawJson error:NULL];  
    NSLog(@"Dict: %@",jsonObject);
    list = [jsonObject objectForKey:@"book"];
    NSLog(@"List: %@",list);
    [rawJson release];
}