I’m using the SBJSON parser to parse a JSON response:
{"status":0,"sessions":[{"name":"kldlksdklsdkl","active":false,"status":"saved","type":"web","key":"30228ee71f09b93aaa2d1738","contributor_id":"lance","created_at":"Mon May 02, 2011 02:35 PM","closed_at":"Mon May 02, 2011 02:46 PM"}{"name":"Blahieririe","active":false,"status":"saved","type":"web","key":"dbd2bbcc8681bba6a6532051","contributor_id":"lance","created_at":"Mon May 02, 2011 01:42 PM","closed_at":"Mon May 02, 2011 02:34 PM"},{"name":"Jim","active":false,"status":"saved","type":"web","key":"ec5bcf18356a29bb4490841f","contributor_id":"lance","created_at":"Fri April 29, 2011 02:37 PM","closed_at":"Fri April 29, 2011 02:38 PM"}]}
from a server using this code:
NSArray *sessionsArray = [dictionary objectForKey:@"sessions"];
NSArray *tempArray = [[NSArray alloc] init];
for(NSString *item in sessionsArray){
NSLog(@"Session Found: \'%@\'",item);
NSDictionary *myDictionary = [item JSONValue];
}
I’m getting a nice array from my JSON but when I try to put each piece into an NSDictionary, it gives me an exception and I NSLoged it and discovered that the quotation marks are being removed from some of the keys and or values as seen here:
{
active = 1;
"contributor_id" = lance;
"created_at" = "Mon May 02, 2011 03:26 PM";
key = e10e5feeea3425ae213cb4cc;
name = "JSON TEST";
status = active;
type = web;
}
is it a bug in the JSON parser? or am I doing something stupid?
The quotation marks aren’t ‘being removed’.
In JSON, every string is quoted but the quotes themselves are not part of the strings. For instance, if you write
the corresponding session name won’t show up with quotation marks.
When you
NSLog()a dictionary or an array, Cocoa uses the NeXTSTEP property list format to represent the dictionary/array. In this format, quotes are optional if the values are simple words.That said, you should enumerate those JSON data as follows: