在 JSON 个文件 iOS 中计数 Parents 个标签

Count Parents tag in JSON file iOS

实际上我打算做一个项目,其中一些表情符号将显示在某些 UIView 中,用户首先必须从服务器下载这些文件(图像和 JSON)。这是我的 JSON 文件:(它不是固定的,可能会有更多 Parent 的标签 "Frames",将来会添加)

{
    "Frames1":
    [{"Image":"smiley64_1_1.png", "Time":"0.04"},
     {"Image":"smiley64_1_2.png", "Time":"0.04"},
     {"Image":"smiley64_1_3.png", "Time":"0.04"},
     {"Image":"smiley64_1_4.png", "Time":"0.04"},
     {"Image":"smiley64_1_5.png", "Time":"0.04"},
     {"Image":"smiley64_1_6.png", "Time":"0.04"},
     {"Image":"smiley64_1_7.png", "Time":"0.04"},
     {"Image":"smiley64_1_8.png", "Time":"0.04"},
     {"Image":"smiley64_1_9.png", "Time":"0.04"},
     {"Image":"smiley64_1_10.png", "Time":"0.04"},
     {"Image":"smiley64_1_11.png", "Time":"0.04"},
     {"Image":"smiley64_1_12.png", "Time":"0.04"},
     {"Image":"smiley64_1_13.png", "Time":"0.04"},
     {"Image":"smiley64_1_14.png", "Time":"0.04"},
     {"Image":"smiley64_1_15.png", "Time":"0.04"},
     {"Image":"smiley64_1_16.png", "Time":"0.04"},
     {"Image":"smiley64_1_17.png", "Time":"0.04"},
     {"Image":"smiley64_1_18.png", "Time":"0.04"},
     {"Image":"smiley64_1_19.png", "Time":"0.04"},
     {"Image":"smiley64_1_20.png", "Time":"0.04"}],

    "Frames2":
    [{"Image":"smiley64_2_1.png", "Time":"0.04"},
     {"Image":"smiley64_2_2.png", "Time":"0.04"},
     {"Image":"smiley64_2_3.png", "Time":"0.04"},
     {"Image":"smiley64_2_4.png", "Time":"0.04"},
     {"Image":"smiley64_2_5.png", "Time":"0.04"},
     {"Image":"smiley64_2_6.png", "Time":"0.04"},
     {"Image":"smiley64_2_7.png", "Time":"0.04"},
     {"Image":"smiley64_2_8.png", "Time":"0.04"},
     {"Image":"smiley64_2_9.png", "Time":"0.04"},
     {"Image":"smiley64_2_10.png", "Time":"0.04"},
     {"Image":"smiley64_2_11.png", "Time":"0.04"},
     {"Image":"smiley64_2_12.png", "Time":"0.04"},
     {"Image":"smiley64_2_13.png", "Time":"0.04"},
     {"Image":"smiley64_2_14.png", "Time":"0.04"},
     {"Image":"smiley64_2_15.png", "Time":"0.04"},
     {"Image":"smiley64_2_16.png", "Time":"0.04"},
     {"Image":"smiley64_2_17.png", "Time":"0.04"},
     {"Image":"smiley64_2_18.png", "Time":"0.04"},
     {"Image":"smiley64_2_19.png", "Time":"0.04"},
     {"Image":"smiley64_2_20.png", "Time":"0.04"}]
}

这里对于 "Frames1" parents 标签,我分别得到 Image & Time child 键的值,并将它们放在两个单独的 NSMUtableArrayimageNamesArray & durationArray.

-(void) retriveData
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"smiley64" 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
    NSArray *objArray = [dictionary objectForKey:@"Frames1"];
    NSLog(@"COUNT %@", objArray);

    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"]];
    }
}

输出:
imageNamesArray: smiley64_1_1.png, smiley64_1_2.png,....smiley64_1_20.png
durationArray: 0.04, 0.04,............0.04

现在我要算的是"Parents tag" (Frames)。有了它,我将显示每个 Parent 的 child 标签,这些标签是图像和它们的 timeDuration 分开 UIImageView 作为表情符号。

像这样:

for (int i = 1; i < 3; i++)
{
    NSString *Parents = [NSString stringWithFormat:@"Frames%i", i];
    NSLog(@"hola %@", Parents);

    //this is your loop array
    //NSArray *objArray = [dictionary objectForKey:@"Frames1"];
    NSArray *objArray = [dictionary objectForKey:Parents];
    //NSLog(@"COUNT %@", objArray);

    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 %@", self.imageNamesArray);
    NSLog(@"durationArray %@", self.durationArray);
}

但是我不知道怎么算,我的JSON中有多少个parents标签,比如Frames? 如果有什么想知道的请问我。必须感谢任何形式的帮助。非常感谢。

完整代码:

/*--------------------- Retrive the data from JSON (Down) ---------------------*/
-(void) retriveData
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"smiley64" ofType:@"json"];
    NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];

    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    dictionary = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];

    for (int i = 1; i < 3; i++)
    {
        NSString *Parents = [NSString stringWithFormat:@"Frames%i", i];
        NSLog(@"hola %@", Parents);

        //this is your loop array
        //NSArray *objArray = [dictionary objectForKey:@"Frames1"];
        NSArray *objArray = [dictionary objectForKey:Parents];
        //NSLog(@"COUNT %@", objArray);

        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 %@", self.imageNamesArray);
        NSLog(@"durationArray %@", self.durationArray);
    }

}
/*--------------------- Retrive the data from JSON (Up) ---------------------*/


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self retriveData];

    count = 0;
    [self imageAnimation];
}


/*--------------------- Perform the sequence animation (Down) ---------------------*/
-(void) imageAnimation
{
    if (count == imageNamesArray.count)
    {
        count = 0;
        [self imageAnimation];
    }
    else
    {
        self.imageView.image = [UIImage imageNamed:[self.imageNamesArray objectAtIndex:count]];
        [self performSelector:@selector(imageAnimation) withObject:nil afterDelay:[[self.durationArray objectAtIndex:count] doubleValue]];
        count++;
    }
}
/*--------------------- Perform the sequence animation (Up) ---------------------*/

NSDictionary 有方法 allKeys

NSArray *allKeys = [dictionary allKeys];
for (NSUInteger i = 0; i < allKeys.count; i ++) {
     NSArray *imagesArray = [dictionary objectForKey:[allKeys objectAtIndex:i]];
     //parse your images
}

希望对您有所帮助。

要仅计算名为 "Frames..." 的 json 对象的键,您可以使用此方法:

 //The dictionary parameter should be your dictionary from your NSJSONSerialization
-(NSInteger)countFramesKeysFromDictionary:(NSDictionary *)dictionary{
    NSInteger framesCounter = 0;
    for (id key in [dictionary allKeys]) {
        NSString *keyAsString = (NSString *) key;
        if ([keyAsString containsString:@"Frames"]) {
            framesCounter++;
        }
    }
    return framesCounter;
}

如果您的 json 词典中最高级别的所有键都命名为 "Frames..",就像上面的示例一样,您可以使用

[[dictionary allKeys] count];

就像前面提到的 Pavel Gatilov。