1

I´m using the SBJson framework the first time, and I´ve got a big problem, the following code always returns null. The NSDictionary is correctly filled (I printed every value out!).

NSDictionary *nsDictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSString *jsonString = [jsonWriter stringWithObject:nsDictionary];
NSLog(@"%@", jsonString);  

I also tried this:

NSDictionary *nsDictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];
SBJsonWriter *jsonWriter = [SBJsonWriter new];
NSString *jsonString = [jsonWriter stringWithObject:nsDictionary];
NSLog(@"%@", jsonString);  
  • Have you validated that the JSON you're parsing is valid? Use http://jsonlint.com/ to validate, also consider using http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html instead of SBJSONParser – Devraj Jul 09 '12 at 09:45
  • I can´t use apple´s framework because my app must be compatible with iOS 4.X >. –  Jul 09 '12 at 09:48

1 Answers1

2

The dictionary may contains objects that cannot be written by SBJsonWriter.

You may use -[SBJsonWriter stringWithObject:error:] to get the failure's reason.

NSDictionary *nsDictionary = [NSDictionary dictionaryWithObjects:values forKeys:keys];
SBJsonWriter *jsonWriter = [SBJsonWriter new];
NSError *error = nil;
NSString *jsonString = [jsonWriter stringWithObject:nsDictionary error:&error];
if ( ! jsonString ) {
    NSLog(@"Error: %@", error);
}
Nicolas Bachschmidt
  • 6,475
  • 2
  • 26
  • 36