jkigel
jkigel

Reputation: 1592

iOS JSONKit objectWithData returns null

I'm using JSONKit to parse JSON strings, for some reason when trying to assign the JSON string into NSDictionary it returns null


JSONDecoder *jsonDecoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];        
NSData *jsonData = [NSData dataWithBytes:[jsonString UTF8String] length:[jsonString length]];
NSDictionary *tempDict = [jsonDecoder objectWithData:jsonData];

jsonString holds the content, tempDict equals NULL

Thanks!

Upvotes: 0

Views: 1546

Answers (4)

Singh
Singh

Reputation: 2161

i am not sure , but you need to check if your json is a valid json , for this you can use http://jsonlint.com/ . i faced this similar problem sometimes because of invalid json . hope this helps.

Upvotes: 2

brynbodayle
brynbodayle

Reputation: 6626

If you want to support 4.x don't use NSJSONSerialization. It's only available in 5.0.

Instead you just need to use JSONKit like so:

 NSDictionary *tempDict = [jsonString objectFromJSONString];

You don't need to convert your JSON string into NSData to serialize it.

Upvotes: 2

Ron
Ron

Reputation: 1047

Why use jsonKit? iOS has a very good JSONSerialization class... You can use it like this:

NSData *returnData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonParsingError = nil;

NSDictionary *returnDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&jsonParsingError];

if (!jsonParsingError) {

    //Do your stuff here

}

Good luck!

Upvotes: 1

Tsuneo Yoshioka
Tsuneo Yoshioka

Reputation: 7874

How about using NSJSONSerialization ?

NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * tempDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];

Upvotes: -1

Related Questions