我是 iOS 开发的新手,我正在尝试解析本地 Json 文件,例如
{"quizz":[{"id":"1","Q1":"When Mickey was born","R1":"1920","R2":"1965","R3":"1923","R4","1234","response","1920"},{"id":"1","Q1":"When start the cold war","R1":"1920","R2":"1965","R3":"1923","rep4","1234","reponse","1920"}]}
这是我的代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
// Parse the string into JSON
NSDictionary *json = [myJSON JSONValue];
// Get all object
NSArray *items = [json valueForKeyPath:@"quizz"];
NSEnumerator *enumerator = [items objectEnumerator];
NSDictionary* item;
while (item = (NSDictionary*)[enumerator nextObject]) {
NSLog(@"clientId = %@", [item objectForKey:@"id"]);
NSLog(@"clientName = %@",[item objectForKey:@"Q1"]);
NSLog(@"job = %@", [item objectForKey:@"Q2"]);
}
我在此站点上找到了一个示例,但出现以下错误
-JSONValue 失败。错误是:在对象键之后不需要令牌“值分隔符”。