Cocoa-Touch – 如何parsing本地Json文件

我在iOS开发新手,我试图parsing一个本地的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"]); } 

我在这个网站上find一个样本,但我得到以下错误

-JSON值失败。 错误是:令牌'值分隔符'不在对象键之后。

JSON有一个严格的键/值表示法,R4和响应的键/值对是不正确的。 尝试这个:

 NSString *jsonString = @"{\"quizz\":[{\"id\":\"1\",\"Q1\":\"When Mickey was born\",\"R1\":\"1920\",\"R2\":\"1965\",\"R3\":\"1923\",\"R4\":\"1234\",\"response\":\"1920\"}]}"; 

如果你从文件中读取string,你不需要所有的斜杠
你的文件会是这样的:

{“quizz”:[{“id”:“1”,“Q1”:“米奇出生时”,“R1”:“1920”,“R2”:“1965”,“R3”:“1923” “R4”:“1234”,“响应”:“1920”,{“id”:“1”,“Q1”:“开始冷战时”,“R1”:“1920”,“R2” “1965年”, “R3”: “1923”, “R4”: “1234”, “效应初探”: “1920”}]}


我testing了这个代码:

 NSString *jsonString = @"{\"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\",\"R4\":\"1234\",\"reponse\":\"1920\"}]}"; NSLog(@"%@", jsonString); NSError *error = nil; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error]; 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"]); } 

我得到的印象是,你复制旧的代码,因为你不使用苹果的序列化和枚举器,而不是快速枚举 。 整个枚举的东西可以写成简单的

 NSArray *items = [json valueForKeyPath:@"quizz"]; for (NSDictionary *item in items) { NSLog(@"clientId = %@", [item objectForKey:@"id"]); NSLog(@"clientName = %@",[item objectForKey:@"Q1"]); NSLog(@"job = %@", [item objectForKey:@"Q2"]); } 

或者甚至更喜欢使用基于块的枚举 ,如果需要快速和安全的枚举,则可以添加索引。

 NSArray *items = [json valueForKeyPath:@"quizz"]; [items enumerateObjectsUsingBlock:^(NSDictionary *item , NSUInteger idx, BOOL *stop) { NSLog(@"clientId = %@", [item objectForKey:@"id"]); NSLog(@"clientName = %@",[item objectForKey:@"Q1"]); NSLog(@"job = %@", [item objectForKey:@"Q2"]); }]; 
 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]; 

使用jsonlint.com查找您的JSONstring中的错误。
在这种情况下,它说你在"R4"附近有无效的JSON

在你的json文件中似乎有一个错字。

更换
"R4","1234","response","1920""R4":"1234","response":"1920"

"rep4","1234","reponse","1920""rep4":"1234","response":"1920"

Swift 2.3我使用实用工具将JSON文件转换为字典:

 func getDictionaryFromJSON(jsonFileName: String) -> [String: AnyObject]? { guard let filepath = NSBundle.mainBundle().pathForResource(jsonFileName, ofType: "json") else { return nil } guard let data = NSData(contentsOfFile: filepath) else { return nil } do { let dict = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] return dict } catch { print(error) return nil } }