이것은 내 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);
그러나 (내 말은 ) 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);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다