Search code examples
objective-ciosnullsbjson

iOS SBJsonWriter returns null


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);  

Solution

  • 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);
    }