我是Json开发人员的新手,我正在尝试解析本地iOS文件,例如
{"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失败。错误是:对象键后不应有标记“值分隔符”。
发布于 2014-06-30 18:25:08
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"json"];
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSError *error = nil;
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
发布于 2012-08-28 06:56:22
使用jsonlint.com查找JSON字符串中的错误。
在本例中,它显示在"R4"
附近有无效的JSON
发布于 2012-08-28 06:55:43
您的json文件中似乎有一个拼写错误。
替换
使用"R4":"1234","response":"1920"
的"R4","1234","response","1920"
和
使用"rep4":"1234","response":"1920"
的"rep4","1234","reponse","1920"
https://stackoverflow.com/questions/12150595
复制相似问题