我需要获取特定对象值(A,B,C,D)和相关键值(@“name”)。我在下面发布了示例代码和响应。请帮帮我。
NSString *combined = URL;
NSURL *url = [[NSURL alloc] initWithString:combined];
NSData *responseData=[NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *responsData = [jsonDictionary objectForKey:@"response"];
// GET A,B,C Object values
NSDictionary *d1 = responsData.firstObject;
NSEnumerator *enum1 = d1.keyEnumerator;
NSArray *firstObject = [enum1 allObjects];
我的JSON回复:
response : [ {
A = [ {
name : tango
}
{
name : ping
}
]
B = [ {
name : tango
}
{
name : ping
}
]
} ]
答案 0 :(得分:1)
简单地
for(NSDictionary *dict in firstObject){
NSLog(@"%@",[dict objectForKey:@"name"]);
}
答案 1 :(得分:1)
您可以使用以下命令获取所有名称的列表:
NSMutableArray *names = [[NSMutableArray alloc] init];
for (NSDictionary *dict in responsData) {
[dict enumerateKeysAndObjectsUsingBlock: ^(id key, id obj, BOOL *stop) {
NSArray *valueArray = (NSArray *)obj;
for (NSDictionary * namesDict in valueArray) {
[names addObject:namesDict[@"name"]];
}
}];
}
输出:
NSLog(@"Names %@",names);
探戈,平,探戈,平。
希望有所帮助!