这是我的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
名称NSMutableArray
中保存imageNamesArray
个键的每个值(此处共20个),并在另一个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);
但是,它会保存Image
第一行中的所有imageNamesArray
键值(我的意思是[imageNamesArray objectAtIndex:0]
)。同样适用于durationArray
。怎么想我这样做?
非常感谢。
OUT PUT将
imageNamesArray = 1.png, 2.png...........20.PNG
durationArray = 0.04, 0.04...............0.04
答案 0 :(得分:1)
您可以浏览帧数组中的每个NSDictionary
,并可以将对象添加到相应的数组中。
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);
答案 1 :(得分:1)
你应该使用第一帧对象作为数组并在其中循环。
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);