網頁

2012年5月16日 星期三

Xcode test JSON

  
  "test": [
    {
      "question": {
        "name": "A",
        "chinese": "這是JSON問句測試?"
      },
      "answer": {
        "name": "B",
        "selection": [
          {
            "chinese": "這是JSON答案測試選項A。"
          },
          {
            "chinese": "這是JSON答案測試選項B。",
            "answer": "YES"
          }
        ]
      }
    }
   ]
  
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    int rowCount =0;
    
    switch (section) {
        case 0:
            rowCount = 1; //問句
            break;
        case 1:
            rowCount = [[[[self.tests objectAtIndex: 0] objectForKey: @"answer"] objectForKey: @"selection"] count]; //答案選項
            break;
        default:
            break;
    }
    
    return rowCount;
}
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    // Configure the cell...
    

    if (indexPath.section==0) {
        
        NSDictionary *each = [self.tests objectAtIndex:0];
        
        NSString *text = [[NSString alloc] initWithFormat: @"Q: %@", 
                          [[each objectForKey: @"question"] objectForKey: @"chinese"]];
        cell.textLabel.text = text;
    }
    else {
        
        
        NSDictionary *eachJSON = [[[[self.tests objectAtIndex: 0] 
                                    objectForKey: @"answer"] 
                                   objectForKey: @"selection"] 
                                  objectAtIndex: indexPath.row];
        
        NSLog(@" eachJSON selection  : %@", eachJSON ); 
        
        NSLog(@" eachTKB  chinese: %@",[eachJSON objectForKey: @"chinese"]);
        
        cell.textLabel.text = [eachJSON objectForKey: @"chinese"];
    }
          
    
    return cell;
}
  
2012-05-17 10:46:04.346 multiNodeJSON[1733:f803]  eachJSON selection  : {
    chinese = "\U9019\U662fJSON\U7b54\U6848\U6e2c\U8a66\Uff21\U3002";
}
2012-05-17 10:46:04.347 multiNodeJSON[1733:f803]  eachTKB  chinese: 這是JSON答案測試A。

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。