JSON ha una chiave/notazione strict, le coppie chiave/valore per R4 e la risposta non sono corrette. Prova questo:
NSString *jsonString = @"{\"quizz\":[{\"id\":\"1\",\"Q1\":\"When Mickey was born\",\"R1\":\"1920\",\"R2\":\"1965\",\"R3\":\"1923\",\"R4\":\"1234\",\"response\":\"1920\"}]}";
Se si legge la stringa da un file, non è necessario tutte le barre
Il file potrebbe essere qualcosa di simile:
{ "quiz": [{"id": "1", "Q1": "Quando Mickey era nato", "R1": "1920", "R2": "1965", "R3": "1923", "R4": "1234", "risposta": "1920"}, {"id": "1", "Q1": "Quando inizia la guerra fredda" "," R1 ":" 1920 "," R2 ":" 1965 "," R3 ":" 1923 "," R4 ":" 1234 "," risposta ":" 1920 "}]}
ho provato con questo codice:
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"]);
}
ho avuto l'impressione, che è stato copiato il vecchio codice, come non si utilizza la serializzazione di mela e un enumeratore invece di Fast Enumeration. La roba intera enumerazione potrebbe essere scritto semplice come
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"]);
}
o anche più elaborato con block based enumeration, hwere avete additionaly un indice, se necessario, per l'enumerazione veloce e sicuro.
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"]);
}];
Il tuo JSON è non valido. –