【问题标题】:Parse a JSON array of dictionaries in a tableview在 tableview 中解析字典的 JSON 数组
【发布时间】:2011-07-11 20:41:34
【问题描述】:

我被困在将 dicts 的 JSON 数组 (http://www.cjs-design.nl/json.php) 解析为 tableview 的点上。我只想显示标题的第一个,稍后会弄清楚详细视图。

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;

}

【问题讨论】:

    标签: iphone ios json nsarray nsdictionary


    【解决方案1】:

    在某个地方,即 viewDidLoad,你需要解析你的 JSON。然后实现 UITableView 的 dataSource 方法。

    - (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;
    }
    

    【讨论】:

      【解决方案2】:
      - (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];
      }
      

      当我对它们进行 NSlog 时,它们都打印 Null,所以字符串不会被解析?该字符串包含一个数组字典。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-05
        相关资源
        最近更新 更多