gpt4 book ai didi

ios - 获取 JSON 中所有对象的特定键值

转载 作者:行者123 更新时间:2023-11-28 21:49:03 27 4
gpt4 key购买 nike

这是我的 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姓名imageNamesArrayTime 的每个值键入另一个 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 也一样.我该怎么办?非常感谢。

输出将是

imageNamesArray = 1.png, 2.png...........20.PNG
durationArray = 0.04, 0.04...............0.04

最佳答案

您应该使用第一个帧对象作为数组并在其中循环。

        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);

关于ios - 获取 JSON 中所有对象的特定键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29004132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com