Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

objective c - Cocoa-Touch - How parse local Json file

I'm newbie in iOS dev and I'm trying to parse a local Json file such as

{"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"}]}

here is my code:

 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"]);
}

I found on this site a sample but I get the following error

-JSONValue failed. Error is: Token 'value separator' not expected after object key.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

JSON has a strict key/Value notation, your key/value pairs for R4 and response are not correct. Try this:

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

If you read the string from a file, you don't need all the slashes
Your file would be something like this:

{"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"}]}


I tested with this code:

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"]);
}

I got the impression, that you copied old code, as you are not using apple's serialization and a Enumerator instead of Fast Enumeration. The whole enumeration stuff could be written simple as

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"]);
}

or even fancier with block based enumeration, hwere you have additionaly an index if needed to the fast and secure 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"]);
}];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...