0

Scenario: Ruby on Rails app that returns JSON to an iOS application using json-framework to parse the JSON and push the data onto Core Data objects.

Problem: Many of attributes returned from in the JSON can be null and a large number of them are not simple strings (e.g., they are DateTimes with timezones, integers, floats, etc...).

Question: What is the most efficient way to handle such JSON? Does json-framework (or something else perhaps) have any helpers to make parsing such data easier ... -or- ... do I simply gotta do [NSNull null] checks on each and every attribute and if not null do the appropriate conversion to NSDate, NSNumber or whatever?

Thanks -wg

PengOne
  • 48,188
  • 17
  • 130
  • 149
wgpubs
  • 8,131
  • 15
  • 62
  • 109

1 Answers1

2

With regards to handling Null values, I suggest making use of a category on NSDictionary:

TouchJSON, dealing with NSNull

Community
  • 1
  • 1
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92
  • +1 You can pass messages to `nil` and the code will continue to work (it just doesn't do anything). – Alex Reynolds Aug 18 '11 at 21:31
  • I kinda like this one too that I created for Core Data: - (NSNumber *) numberAsBoolForKey:(id)key { // check for null return [self objectForKeyNotNull:key] == nil ? [NSNumber numberWithBool:NO] : [NSNumber numberWithBool:[[self objectForKey:key] boolValue]]; } – wgpubs Aug 21 '11 at 01:16