Get json example:
(
{
"au_id" = 2;
"au_name" = viswanathan;
items = (
{
"aef_food_category" = 2;
"aef_id" = 3;
},
{
"aef_food_category" = 1;
"aef_id" = 8;
},
{
"aef_food_category" = 4;
"aef_id" = 10;
}
);
},
{
"au_id" = 3;
"au_name" = "Arvind Kumar";
items = (
{
"aef_food_category" = 4;
"aef_id" = 9;
},
{
"aef_food_category" = 2;
"aef_id" = 1;
}
);
},
{
"au_id" = 4;
"au_name" = abi;
items = (
{
"aef_food_category" = 1;
"aef_id" = 6;
}
);
}
)
Convert to section and array:
(
{
"au_id" = 2;
"au_name" = viswanathan;
items = (
{
"aef_food_category" = 2;
"aef_id" = 3;
},
{
"aef_food_category" = 1;
"aef_id" = 8;
},
{
"aef_food_category" = 4;
"aef_id" = 10;
}
);
},
{
"au_id" = 3;
"au_name" = "Arvind Kumar";
items = (
{
"aef_food_category" = 4;
"aef_id" = 9;
},
{
"aef_food_category" = 2;
"aef_id" = 1;
}
);
},
{
"au_id" = 4;
"au_name" = abi;
items = (
{
"aef_food_category" = 1;
"aef_id" = 6;
}
);
}
)
Convert to section and array:
NSMutableDictionary *sections;
for (NSDictionary *dataDict in jsonObjects)
{
NSString *title_str = [dataDict objectForKey:@"au_name"];
[sections setValue:[[NSMutableArray alloc] init] forKey:title_str];
}
for(NSDictionary *dataDict in jsonObjects) {
id items = [dataDict objectForKey:@"items"];
NSString *str_user_id = [dataDict objectForKey:@"au_id"];
for (NSDictionary *itemDict in items) {
NSString *str_foodid = [itemDict objectForKey:@"aef_id"];
NSString *str_foodcate = [itemDict objectForKey:@"aef_food_category"];
NSDictionary *foodDict = [NSDictionary dictionaryWithObjectsAndKeys:
str_user_id, @"au_id",
str_foodid, @"aef_id",
str_foodcate, @"aef_food_category",
nil];
[[sections objectForKey:[dataDict objectForKey:@"au_name"]] addObject:foodDict];
}
}
Result:
{
"Arvind Kumar" = (
{
"aef_food_category" = 4;
"aef_id" = 9;
"au_id" = 3;
},
{
"aef_food_category" = 2;
"aef_id" = 1;
"au_id" = 3;
}
);
abi = (
{
"aef_food_category" = 1;
"aef_id" = 6;
"au_id" = 4;
}
);
viswanathan = (
{
"aef_food_category" = 2;
"aef_id" = 3;
"au_id" = 2;
},
{
"aef_food_category" = 1;
"aef_id" = 8;
"au_id" = 2;
},
{
"aef_food_category" = 4;
"aef_id" = 10;
"au_id" = 2;
}
);
}
// Table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[sections allKeys] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [book objectForKey:@"aef_id"];
cell.detailTextLabel.text = [book objectForKey:@"aef_food_category"];
return cell;
}
No comments:
Post a Comment