-2
Departments:[
  {
     "name":"Designing",
     "id":"1.1",
     Employees:[
     {
       "name":"Ramesh",
       "id":"1.1.1",
       "salary":"4lakhs"
     },
     {
       "name":"Suresh",
       "id":"1.1.2",
       "salary":"4lakhs"
     },
     {
       "name":"Mukesh",
       "id":"1.1.3",
       "salary":"4lakhs"
     }
     ]
     }
4

3 回答 3

2

做这个:

 NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsondata options:kNilOptions error:&error];

NSArray *arrDepartment = [jsonDictionary objectForKey:@"Departments"];

现在你有 arrDepartment,它是 tableview 数据源。所以相应地使用

进一步让员工像这样:

NSArray *arrEmployees = [[arrDepartment objectAtIndex:0]  objectForKey:@"Employees"];
于 2012-09-25T09:53:50.510 回答
0

在你的 .h 文件中取 NSMutableArray :

@property (nonatomic, retain) NSMutableArray *employeeData;

在 .m 文件中

@synthesize employeeData;

然后对您的代码进行必要的更改。

-(void)getEmpData
{

 self.employeeData=[[NSMutableArray alloc] init];


     NSData *jsonData = @"Your Json Data";

            NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

        NSArray *arrDepartment = [jsonDictionary objectForKey:@"Departments"];

        NSArray *arrEmployees = [[arrDepartment objectAtIndex:0] objectForKey:@"Employees"];


          self.employeeData= [arrEmployees mutableCopy];  
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

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

NSDictionary *dict =[self.employeeData objectAtIndex:indexPath.row];

             NSLog(@"empname=%@", [dict objectForKey:@"name"]); 
             NSLog(@"empid=%@",[dict objectForKey:@"id"]); 
             NSLog(@"salary=%@",[dict objectForKey:@"salary"]);           

}

于 2012-09-25T11:55:13.847 回答
0

使用SBJSON解析它并在 NSMutableArray 中获取这个Departments数组,然后根据indexpath.row;

并且您的数据应该是这种格式使用http://jsonformatter.curiousconcept.com/来检查 json 格式

{
   "Departments":[
      {
         "name":"Designing",
         "id":"1.1",
         "Employees":[
            {
               "name":"Ramesh",
               "id":"1.1.1",
               "salary":"4lakhs"
            },
            {
               "name":"Suresh",
               "id":"1.1.2",
               "salary":"4lakhs"
            },
            {
               "name":"Mukesh",
               "id":"1.1.3",
               "salary":"4lakhs"
            }
         ]
      }
   ]
}
于 2012-09-25T09:51:22.857 回答