3

I'm still learning JSON, and using SBJSON. How would I go about taking an NSDictionary and converting it into a JSON object similar to this:

{"id":"123","list":"456","done":1,"done_date":1305016383}
Nazik
  • 8,696
  • 27
  • 77
  • 123
spentak
  • 4,627
  • 15
  • 62
  • 90

3 Answers3

6

Use the category that SBJSON adds to NSDictionary:

NSString *jsonString = [dictionary JSONRepresentation];

Just name the keys and values appropriately in the dictionary and SBJSON will do the work for you

Circumflex
  • 710
  • 4
  • 11
3

There is a JSON-API (since iOS5 and OS X 10.7) in the official iOS-SDK it's called:

+ (NSData *)dataWithJSONObject:(id)obj
                       options:(NSJSONWritingOptions)opt
                         error:(NSError **)error

See Guillaume's post for more information how to use it: https://stackoverflow.com/a/9020923/261588

Works like a charm.

Community
  • 1
  • 1
RhodanV5500
  • 1,087
  • 12
  • 16
2

For those that are using JSONKit instead of SBJSON:

JSONKit also provides a category for NSDictionary

NSString *jsonString = [dictionary JSONStringWithOptions:JKSerializeOptionNone error:nil]
Robert Wagstaff
  • 2,664
  • 1
  • 27
  • 40