Generate a JSON string using Objective-C
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 t开发者_如何学Goo write myself, or is there something simple already out there?
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];
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];
The interface for this framework seems pretty straight forward to use for generating JSON.
精彩评论