Thursday, November 4, 2010

null strings in JSONRepresentation

NSDictionary supports the JSONRepresentation method which is quite useful for generating JSON to send to servers. I recently had an issue where if one of my object's members was null, the JSONRepresentation would fail. If I set the member NSString to @"", then "" would get sent across, but what I really wanted was "member":null to be sent.

The solution is to set your object to [NSNull null], and then the correct JSONRepresentation will be created.