1

I know, JSON parsing questions are asked over and over again, but still I can't find any answer to this one.
I've been trying to read and parse a textual JSON file using NSJSONSerialization to no avail.
I've tried using the same JSON data from a NSString and it did work.
Here's the code:

NSError      *error;

NSString     *jsonString1 = [NSString stringWithContentsOfFile:jsonFilePath
                                                      encoding:NSUTF8StringEncoding
                                                         error:&error];
NSData       *jsonData1   = [jsonString1 dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonObject1 = [NSJSONSerialization JSONObjectWithData:jsonData1
                                                            options:0
                                                              error:&error];

NSString     *jsonString2 = @"{\"key\":\"value\"}";
NSData       *jsonData2   = [jsonString2 dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonObject2 = [NSJSONSerialization JSONObjectWithData:jsonData2
                                                            options:0
                                                              error:&error];

- The text file contains one line: {"key":"value"}
- jsonString1 = @"{"key":"value"}"
- jsonString2 = @"{\"key\":\"value\"}"
- jsonData1 is 23 bytes in size
- jsonData2 is 15 bytes in size
- jsonObject1 is nil and I get error code 3840
- jsonObject2 is a valid dictionary

Seems like the problem is with reading the file, since the NSStrings and NSDatas differ, but what am I doing wrong here and how can I fix it?

3
  • Could it be that the file contains some unprintable characters that trigger the failure? What's the size of the file? Commented Jan 19, 2016 at 18:20
  • What's the output of console.log(jsonData1) (in your code) and/or hexdump -C file_name (in Terminal)? Is the file actually UTF-8-encoded (rather than UTF-16 or whatever)? Any invisible characters? Unrelated: you could skip the NSString roundtrip via NSData dataWithContentsOfFile:... Commented Jan 19, 2016 at 18:34
  • As far as length of data differs there must be some unprintable character and it should be \0 otherwise it wouldn't cut the string. Commented Jan 19, 2016 at 20:02

2 Answers 2

1

Most likely you file contains some unprintable characters (e.g. \0) that trigger the failure. Printing the error message will tell you at what position the first invalid characters occurs.

For example, try printing "{\"key\":\u{0000}\"value\"}" and you'll seem to get a valid JSON, however decoding it fails.

Sign up to request clarification or add additional context in comments.

1 Comment

You are all correct. The file's encoding wasn't UTF-8. Somehow it didn't cross my mind that TextEdit on Mac might save ".txt" files with a different encoding. Anyway, if anyone encounters this issue with files created with TextEdit, go to "TextEdit->Preferences->Open and Save" and set "Plain Text File Encoding" as UTF-8. Thanks, everyone.
0

I always do a check on the return value when doing anything with NSUTF8StringEncoding and if nil, then try NSASCIIStringEncoding:

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (jsonString == nil) {
    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];
}

return jsonString;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.