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

标签 ios objective-c json nsmutablearray nsmutabledictionary

这是我的 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/

相关文章:

ios - Objective-C 表示法 - UINavigationBar

iphone - NSMutableDictionary 从不同的类访问

python - 将两个 python 数据处理脚本组合成一个工作流

android - 无法使用 loopj AsyncHttpClient 和授权 header 进行 POST

objective-c - UIButton 作为 MKAnnotationView 中的 subview 不起作用

ios - 升级到 Xcode-beta 7 后出现奇怪的编译错误

ios - 在 UICollectionView 中加载用户相册时内存增长失控

ios - 当按钮是 iOS 中自定义单元格的 subview 时,如何动态更改按钮文本?

ios - 按下 iOS 9 后退按钮后重新加载 View

javascript - React组件根据URL显示动态数据