我是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失败。错误是:在对象键之后不期望令牌'值分隔符'。
答案 0 :(得分:14)
JSON具有严格的键/值表示法,R4和响应的键/值对不正确。试试这个:
NSString *jsonString = @"{\"quizz\":[{\"id\":\"1\",\"Q1\":\"When Mickey was born\",\"R1\":\"1920\",\"R2\":\"1965\",\"R3\":\"1923\",\"R4\":\"1234\",\"response\":\"1920\"}]}";
如果您从文件中读取字符串,则不需要所有斜杠
你的文件是这样的:
{“quizz”:[{“id”:“1”,“Q1”:“当米奇是 生 “ ”R1“: ”1920“, ”R2“: ”1965“, ”R3“: ”1923“, ”R4“: ”1234“, ”响应“: ”1920“},{ ”ID“:” 1" , “Q1”:“当 开始感冒 战”, “R 1”: “1920”, “R2”: “1965”, “R3”: “1923”, “R4”: “1234”, “效应初探”: “1920”}]}
我测试了这段代码:
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"]);
}
我得到的印象是,您复制了旧代码,因为您没有使用Apple的序列化和枚举器而不是Fast Enumeration。整个枚举的东西可以简单地写成
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"]);
}
或者甚至是block based enumeration的爱好者,如果需要快速安全的枚举,你还有一个索引。
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"]);
}];
答案 1 :(得分:4)
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];
答案 2 :(得分:2)
使用jsonlint.com查找JSON字符串中的错误
在这种情况下,它表示您在"R4"
附近有无效的JSON
答案 3 :(得分:0)
json文件中似乎有拼写错误。
替换
"R4","1234","response","1920"
"R4":"1234","response":"1920"
的{{1}}
和
"rep4","1234","reponse","1920"
与"rep4":"1234","response":"1920"
答案 4 :(得分:0)
Swift 2.3 我使用实用程序方法将JSON文件转换为Dictionary:
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
}
}