Question

I'm storing the result of JSONKit parse in a key/value database (LevelDB), but the JSON I'm downloading has some filed set to null, and this won't let you serialize the corresponding generated object (NSArray or NSDictionary), to store it as an object.

¿Any idea how can I deep iterate over a NSSomething (Dictionary or Array) to change those values?

Was it helpful?

Solution

There is a post https://github.com/johnezang/JSONKit/issues/25 that explains how to modify the framework to omit it from dictionaries and array's

OTHER TIPS

Sometime it happened to me that a null value was set to something what was not recognized neither as NSNull nor as NSString. Therefore i´ve replaced all null strings in the json string before i parse it with NSJSONSerialization. I´ve read the data into a NSData object, copied it into a NSString object, replaced the null strings and copied it again into a NSData object as NSJSONSerialization expects a NSData object. Maybe you can write it shorter but it works.

Here´s the code

NSString *jsonPath = [myPath stringByAppendingPathComponent:@"myDataFile.json"];
NSMutableData *myJSON = [[NSMutableData alloc] initWithContentsOfFile:jsonPath];
NSString *jsonString = [[NSString alloc] initWithBytes:myJSON.bytes length:myJSON.length encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"null" withString:@"\"placeholder\""];
NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonParsingError];

after that all previous null occurances will contain a placeholder string.

I parsed the JSON to a mutableObject [[JSONDecoder decoder] mutableObjectWithUTF8String:(const unsigned char *) [json UTF8String] length:[json lengthOfBytesUsingEncoding:NSUTF8StringEncoding] error:nil ]; and then used this code to fix it:

-(id) fixObject: (id) a
{
   if([a isKindOfClass:[NSMutableDictionary class]])
   {
      NSMutableDictionary *ad = a;
      for (NSObject *key in ad)
      {
          if([[ad objectForKey:key] isKindOfClass:[NSMutableDictionary class]] || [[ad objectForKey:key] isKindOfClass:[NSMutableArray class]])
              [self fixObject:[ad objectForKey:key]];
          else 
          {
              if((NSNull *)[ad objectForKey:key] == [NSNull null]) [ad setObject:@"" forKey:key];
          }

      }
  }else if([a isKindOfClass:[NSMutableArray class]])
  {
      NSMutableArray *ar = a;
      for (NSObject *ob in ar)
      {
          if([ob isKindOfClass:[NSMutableDictionary class]] || [ob isKindOfClass:[NSMutableArray class]])
          {
             [self fixObject:ob];
          }
          else if((NSNull *)ob == [NSNull null])
          {
              [ar removeObject:ob];
          }

      }
  }
  return a;
}

If you find a better way to do this, let me know!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top