I know there are a few frameworks to parse JSON, but how can I generate a JSON string in Objective-C? Will it be something I would have to write myself, or is there something simple already out there?
3 Answers
I personally like json-framework's use of categories on NSObject over having to go through TouchJSON's CJSONDataSerializer interface.
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"b" forKey:@"a"];
NSString *json = [dict JSONRepresentation];
Comments
TouchJSON includes a mechanism to take a dictionary and produce JSON from it:
http://github.com/schwa/TouchJSON
The example code from that page:
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"b" forKey:@"a"];
NSError *error = NULL;
NSData *jsonData = [[CJSONDataSerializer serializer] serializeObject:dictionary error&error];
Comments
The interface for this framework seems pretty straight forward to use for generating JSON.