一尘不染

Cocoa-Touch-如何解析本地Json文件

json

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


阅读 247

收藏
2020-07-27

共1个答案

一尘不染

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”:“冷战开始时”,“ 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"]);
}

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