获取 JSON 中所有对象的特定键值
Getting a particular key value for all object in JSON
这是我的 JSON:
{
"Frames":
[
{"Image":"1.png", "Time":"0.04"},
{"Image":"2.png", "Time":"0.04"},
{"Image":"3.png", "Time":"0.04"},
{"Image":"4.png", "Time":"0.04"},
{"Image":"5.png", "Time":"0.04"},
{"Image":"6.png", "Time":"0.04"},
{"Image":"7.png", "Time":"0.04"},
{"Image":"8.png", "Time":"0.04"},
{"Image":"9.png", "Time":"0.04"},
{"Image":"10.png", "Time":"0.04"},
{"Image":"11.png", "Time":"0.04"},
{"Image":"12.png", "Time":"0.04"},
{"Image":"13.png", "Time":"0.04"},
{"Image":"14.png", "Time":"0.04"},
{"Image":"15.png", "Time":"0.04"},
{"Image":"16.png", "Time":"0.04"},
{"Image":"17.png", "Time":"0.04"},
{"Image":"18.png", "Time":"0.04"},
{"Image":"19.png", "Time":"0.04"},
{"Image":"20.png", "Time":"0.04"}
]
}
我想将 Image
键的每个值(这里总共 20 个)保存在一个 NSMutableArray
名称 imageNamesArray
中,并将 Time
键的每个值保存在另一个 NSMutableArray
姓名 durationArray
.
我尝试使用 [dictionary valueForKeyPath:@"Frames.Image"]
,如下所示:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"animation" ofType:@"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
NSMutableDictionary *detailsDictionary = [[NSMutableDictionary alloc] init];
self.imageNamesArray = [[NSMutableArray alloc] init];
self.durationArray = [[NSMutableArray alloc] init];
[self.imageNamesArray addObject:[dictionary valueForKeyPath:@"Frames.Image"]];
[self.durationArray addObject:[dictionary valueForKeyPath:@"Frames.Time"]];
NSLog(@"imageNamesArray %@", [imageNamesArray objectAtIndex:0]);
NSLog(@"durationArray %@", durationArray);
但是,它保存了imageNamesArray
第一行的所有Image
键值(我的意思是[imageNamesArray objectAtIndex:0]
)。 durationArray
也一样。我该怎么办?
非常感谢。
输出将是
imageNamesArray = 1.png, 2.png...........20.PNG
durationArray = 0.04, 0.04...............0.04
可以遍历Frames Array中的Each NSDictionary
,可以将object添加到相应的Array中。
for (NSDictionary *dict in dictionary[@"Frames"]) {
[_imageNamesArray addObject:dict[@"Image"]];
[_durationArray addObject:dict[@"Time"]];
}
NSLog(@"imageNamesArray %@--%ld", _imageNamesArray, _imageNamesArray.count);
NSLog(@"durationArray %@--%ld", _durationArray, _durationArray.count);
您应该使用第一个帧对象作为数组并在其中循环。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"animation" ofType:@"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
//this is your loop array
NSArrray *objArray=[dictionary objectForKey:@"Frames"];
NSMutableDictionary *detailsDictionary = [[NSMutableDictionary alloc] init];
self.imageNamesArray = [[NSMutableArray alloc] init];
self.durationArray = [[NSMutableArray alloc] init];
for (NSDictionary *dic in objArray) {
[self.imageNamesArray addObject:dic[@"Image"]];
[self.durationArray addObject:dic[@"Time"]];
}
NSLog(@"imageNamesArray %@", [imageNamesArray objectAtIndex:0]);
NSLog(@"durationArray %@", durationArray);
这是我的 JSON:
{
"Frames":
[
{"Image":"1.png", "Time":"0.04"},
{"Image":"2.png", "Time":"0.04"},
{"Image":"3.png", "Time":"0.04"},
{"Image":"4.png", "Time":"0.04"},
{"Image":"5.png", "Time":"0.04"},
{"Image":"6.png", "Time":"0.04"},
{"Image":"7.png", "Time":"0.04"},
{"Image":"8.png", "Time":"0.04"},
{"Image":"9.png", "Time":"0.04"},
{"Image":"10.png", "Time":"0.04"},
{"Image":"11.png", "Time":"0.04"},
{"Image":"12.png", "Time":"0.04"},
{"Image":"13.png", "Time":"0.04"},
{"Image":"14.png", "Time":"0.04"},
{"Image":"15.png", "Time":"0.04"},
{"Image":"16.png", "Time":"0.04"},
{"Image":"17.png", "Time":"0.04"},
{"Image":"18.png", "Time":"0.04"},
{"Image":"19.png", "Time":"0.04"},
{"Image":"20.png", "Time":"0.04"}
]
}
我想将 Image
键的每个值(这里总共 20 个)保存在一个 NSMutableArray
名称 imageNamesArray
中,并将 Time
键的每个值保存在另一个 NSMutableArray
姓名 durationArray
.
我尝试使用 [dictionary valueForKeyPath:@"Frames.Image"]
,如下所示:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"animation" ofType:@"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
NSMutableDictionary *detailsDictionary = [[NSMutableDictionary alloc] init];
self.imageNamesArray = [[NSMutableArray alloc] init];
self.durationArray = [[NSMutableArray alloc] init];
[self.imageNamesArray addObject:[dictionary valueForKeyPath:@"Frames.Image"]];
[self.durationArray addObject:[dictionary valueForKeyPath:@"Frames.Time"]];
NSLog(@"imageNamesArray %@", [imageNamesArray objectAtIndex:0]);
NSLog(@"durationArray %@", durationArray);
但是,它保存了imageNamesArray
第一行的所有Image
键值(我的意思是[imageNamesArray objectAtIndex:0]
)。 durationArray
也一样。我该怎么办?
非常感谢。
输出将是
imageNamesArray = 1.png, 2.png...........20.PNG
durationArray = 0.04, 0.04...............0.04
可以遍历Frames Array中的Each NSDictionary
,可以将object添加到相应的Array中。
for (NSDictionary *dict in dictionary[@"Frames"]) {
[_imageNamesArray addObject:dict[@"Image"]];
[_durationArray addObject:dict[@"Time"]];
}
NSLog(@"imageNamesArray %@--%ld", _imageNamesArray, _imageNamesArray.count);
NSLog(@"durationArray %@--%ld", _durationArray, _durationArray.count);
您应该使用第一个帧对象作为数组并在其中循环。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"animation" ofType:@"json"];
NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
dictionary = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
//this is your loop array
NSArrray *objArray=[dictionary objectForKey:@"Frames"];
NSMutableDictionary *detailsDictionary = [[NSMutableDictionary alloc] init];
self.imageNamesArray = [[NSMutableArray alloc] init];
self.durationArray = [[NSMutableArray alloc] init];
for (NSDictionary *dic in objArray) {
[self.imageNamesArray addObject:dic[@"Image"]];
[self.durationArray addObject:dic[@"Time"]];
}
NSLog(@"imageNamesArray %@", [imageNamesArray objectAtIndex:0]);
NSLog(@"durationArray %@", durationArray);