下面我发布了嵌套的JSON响应。我希望将所有键和键值加载到一个tableview中,如下面的UI。请帮帮我!
我的JSON
response : {
ANI = {
name = "anisharmu";
age = "10";
};
ROC = {
name = "rockins";
age = "20";
};
}
MY UI Tableview
|------------------------|
ANI anisharmu - 10
|------------------------|
我的代码
NSError *error;
jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
// get keys from response dictionary
NSMutableArray * key = [[NSMutableArray alloc] initWithArray:[jsonDictionary[@"response"] allKeys]];
// sort as asending order
NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: YES];
key = (NSMutableArray *)[key sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];
// access inner data from dictonary
for (NSString * obj in key) {
NSLog(@"%@",jsonDictionary[@"response"][obj][@"name"]);
}
答案 0 :(得分:0)
这里的回复是字典。
如果您使用的是AFNetworking
,则只需将回复作为NSDictionary
进行访问即可。您可以通过访问密钥来获取值。
NSDictionary *aniDict =[ responseDict objectForKey:@"ANI"];
将返回ANI
键下的对象。
也可以访问嵌套词典的新词典。
NSString *name = [aniDict valueForKey:@"name"];
int age = [[aniDict objectForKey:@"age"] integerValue];
数字和bool是字典中的NSSNumber
个实例,需要解包。
答案 1 :(得分:0)
首先解析响应数据,如下所示:
NSData *responseData;
NSError *error;
NSDictionary *jSONDictonary = [NSJSONSerialization dataWithJSONObject:responseData options:kNilOptions error:&error];
NSArray *allKeys = [jSONDictonary allKeys];
然后在cellforRowAtIndexpath方法中编写以下代码:
cell.mainName.text = [allKeys objectAtIndex:indexpath.row];
cell.namelabel.text = [jSONDictonary valueForKeyPath:[NSString stringWithFormat:@"%@.name",[allKeys objectAtIndex:indexpath.row]]];
cell.ageLabel.text = [jSONDictonary valueForKeyPath:[NSString stringWithFormat:@"%@.age",[allKeys objectAtIndex:indexpath.row]]];
答案 2 :(得分:0)
联系您的脚本制作者并致电他制作此类剧本
response : [
{
id=ANI;
name = "anisharmu";
age = "10";
}
{ id =ROC;
name = "rockins";
age = "20";
}
];
}
NSMutableArray *ArrData=[responseDict objecforKeys:@"response"];//all data in array now just put it in your tableview
int i=[ArrData count];//all keys
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [ArrData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"cell %ld",(long)indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *lbl_id=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
lbl_id.text = [[ArrData objectAtIndex:indexPath.row] objectForKey:@"id"];
[cell.contentView addSubview:lbl_id];
lbl_id.layer.borderColor=[UIColor blueColor].CGColor;
lbl_id.layer.borderWidth=4.0;
[lbl_id release];
UILabel *lblName=[[UILabel alloc] initWithFrame:CGRectMake(10, 40, 100, 40)];
lblName.text = [[ArrData objectAtIndex:indexPath.row] objectForKey:@"name"];
[cell.contentView addSubview:lblName];
lblName.layer.borderColor=[UIColor blueColor].CGColor;
lblName.layer.borderWidth=4.0;
[lblName release];
UILabel *lblage=[[UILabel alloc] initWithFrame:CGRectMake(10, 70, 100, 40)];
lblage.text=[[ArrData objectAtIndex:indexPath.row] objectForKey:@"age"];
[cell.contentView addSubview:lblage];
lblage.layer.borderColor=[UIColor blueColor].CGColor;
[lblage release];
}
return cell;
}