13

我是 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 失败。错误是:在对象键之后不需要令牌“值分隔符”。

4

5 回答 5

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","re​​sponse":"1920"},{"id":"1","Q1":"什么时候开始冷战","R1":"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"]);
}

我的印象是,您复制了旧代码,因为您没有使用苹果的序列化和 Enumerator 而不是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"]);
}

甚至更喜欢基于块的枚举,如果需要快速和安全的枚举,您是否有额外的索引。

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"]);
}];
于 2012-08-27T22:54:24.323 回答
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];
于 2014-06-30T10:25:08.213 回答
2

使用jsonlint.com查找 JSON 字符串中的错误。
在这种情况下,它表示您附近有无效的 JSON"R4"

于 2012-08-27T22:56:22.060 回答
0

您的 json 文件中似乎有错字。

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

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

于 2012-08-27T22:55:42.527 回答
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
    }
}
于 2016-08-18T08:16:32.867 回答