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"
}
]
}
質問する
182 次
3 に答える
2
これを行う:
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsondata options:kNilOptions error:&error];
NSArray *arrDepartment = [jsonDictionary objectForKey:@"Departments"];
これで、テーブルビューのdataSourceであるarrDepartmentができました。したがって、それに応じて使用してください
さらに次のような従業員を取得します。
NSArray *arrEmployees = [[arrDepartment objectAtIndex:0] objectForKey:@"Employees"];
于 2012-09-25T09:53:50.510 に答える
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 に答える
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 に答える