【问题标题】:i have json data given below and i want to display it in a table我有下面给出的 json 数据,我想在表格中显示它
【发布时间】:2012-09-16 19:41:24
【问题描述】:
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"
     }
     ]
     }

【问题讨论】:

  • 你已经在使用 SBJson 框架了吗?
  • 您的实际问题是什么?不知道如何在表格中显示数据,或者无法解析 JSON?
  • 好吧,在这种情况下,有两个不同的问题——“如何在 Objective-C 中解析 JSON”和“如何在 UITableView 中填充数据”

标签: iphone objective-c json


【解决方案1】:

这样做:

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

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

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

进一步获得这样的员工:

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

【讨论】:

  • AFAIK 是总代码。 Prince 只是假设您的 JSON 字符串在 jsondata 中,并且 jsondata 的类型为 NSData*。
  • 你有 arrDepartment 这是tableview dataSource
【解决方案2】:

在你的 .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"]);           

}

【讨论】:

  • @Rajesh 告诉我它是否对您有用,以便对其他人有帮助,您可以通过单击复选标记接受答案。
  • @prasad... 现在我只能打印三件事中的一件(姓名、薪水、身份证)如果我想一次打印三个数据怎么办....
  • @Rajesh 如果您想在表格视图中一次打印 3 个数据,那么您可以使用自定义框架实用地创建 3 个 UILabel 并将它们添加为 cell.contentView 的子视图。更多详情请点击链接:1.stackoverflow.com/questions/4687238/… 2.iphonesdkarticles.com/2009/02/…
【解决方案3】:

使用SBJSON解析它并将这个Departments数组放入NSMutableArray然后根据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"
            }
         ]
      }
   ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-10-22
    • 2021-03-24
    • 2018-03-22
    • 2021-07-09
    相关资源
    最近更新 更多