8

I am trying to fetch data from an url https://dl.dropboxusercontent.com/s/2iodh4vg0eortkl/facts.json

I am getting nil while converting nsdata to nsdictionary.

I used the following code. and I am able to log the data as well. but as soon as I convert it into dictionary it is showing nil.What am I missing here?

I tried nsurlsession and afnetworking as well. getting the same error.

NSError *error;
NSString *url_string = [NSString stringWithFormat: DATA_URL];
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:url_string]];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"json: %@", json);
Sekhar
  • 341
  • 2
  • 13
  • `dataWithContentsOfURL:`: Not recommended, that's blocking the current UI. Also, is `[NSURL URLWithString:url_string]` nil? Did you check `error`? There is question about AFNetworking/NSURLSession and yet you use `dataWithContentsOfURL:`? – Larme Feb 07 '18 at 10:32
  • I just want to know where the problem is.I gave that in the question as the code is simple. – Sekhar Feb 07 '18 at 10:35
  • 1
    I tried your code, the error says: `Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 2643." UserInfo={NSDebugDescription=Unable to convert data to string around character 2643.}`. Doing then `[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] substringWithRange:NSMakeRange(2643-5, 10)]` gives: `ion":"ノare`. That char seems to be invalid. – Larme Feb 07 '18 at 10:35
  • how did you get that error. I validated it with json lint and it was positive. even in the browser it was showing proper json. – Sekhar Feb 07 '18 at 10:40
  • @Sekhar I also checked this url and getting same result, then I checked json structure, it is valid but probably your chars are not valid -> check https://stackoverflow.com/questions/11174130/ios-5-json-parsing-results-in-cocoa-error-3840 – dahiya_boy Feb 07 '18 at 10:40
  • How did I get it? You do `[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];`. Read `error`. It's often full of information when you have an issue. That's the error I gave. JSONLint may states it valid, but iOS use it's own validator (redone), and it seems it's rejecting your JSON. – Larme Feb 07 '18 at 10:42
  • @Sekhar change your all **null** values to empty string, like this -> `""` & then try. – dahiya_boy Feb 07 '18 at 10:42

2 Answers2

12

You have to convert NSDatainto UTF8 before parsing it using NSJSONSerialization.

NSError* error = nil;

NSString *strISOLatin = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
NSData *dataUTF8 = [strISOLatin dataUsingEncoding:NSUTF8StringEncoding];

id dict = [NSJSONSerialization JSONObjectWithData:dataUTF8 options:0 error:&error];
if (dict != nil) {
    NSLog(@"Dict: %@", dict);
} else {
    NSLog(@"Error: %@", error);
}
Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
-2

If you are looking for the Swift equivalent of Jayesh Thanki's Objective-C code, here it is,

let str = String(data: d, encoding: .isoLatin1)
let data8 = str?.data(using: .utf8)
let result = try JSONSerialization.jsonObject(with: data8!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
cptdanko
  • 812
  • 2
  • 13
  • 22