0

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!

jkigel
  • 1,592
  • 6
  • 28
  • 49
  • Could you provide some input that causes the issue? And just to verify, `jsonData` isn't `nil`, but `tempDict` is? – Carl Veazey Sep 30 '12 at 17:46

4 Answers4

2

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.

brynbodayle
  • 6,546
  • 2
  • 33
  • 49
2

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.

Singh
  • 2,151
  • 3
  • 15
  • 30
1

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!

Ron
  • 1,047
  • 13
  • 18
  • 1
    Thanks but NSJSONSerialization is available from iOS 5, I'm using JSONKit because it's support to prior iOS versions – jkigel Sep 29 '12 at 20:48
-1

How about using NSJSONSerialization ?

NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * tempDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
Tsuneo Yoshioka
  • 7,504
  • 4
  • 36
  • 32