2

JSONObjectWithData is returning null without an error if the json string contains a degree sign ° (U+00B0). The json string displays fine if I serve it to my desktop browser.

My code (a category) with a few NSLogs to see what is happening looks like this...

+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString *)urlAddress{
     __autoreleasing NSError* error = nil;
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlAddress] options:NSDataReadingUncached error:&error];
    if (error != nil) {
        NSLog(@"%@",[error localizedDescription]);        
    }   else {
        NSLog(@"No Error: %@", data);  //looks good here. console displays the raw data 
    }

    id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    if (error != nil) {
        NSLog(@"%@",[error localizedDescription]);
        return nil;

    }   else {
        NSLog(@"No Error: %@", [result objectForKey:@"exams"]); //console displays the value ("No Error: value for key...") unless a degree symbol is present in which case it displays No Error: (null)

    }

    return result;

 }

So I am not getting any errors but the JSONOBjectWithData returns null if the received json string contains a degree symbol.

I used NSString with contents of URL to see how xCode is seeing the string and instead of a degree symbol I am getting wht I thiink is an elipsis ∞ symbol. When viewed in a browser the same string is a degree symbol.

Is this a bug? I must be missing something.

Thanks,

John

user278859
  • 10,379
  • 12
  • 51
  • 74
  • Have you tried using a unicode escape sequence instead? – Wug Jul 05 '12 at 18:00
  • 1
    Yes that works, but the bigger problem is that the degree symbol may not be the only character problems I could encounter as the values are user entries in a database. I was hoping I could make xCode handle such characters as I do not know which unicode characters I have to escape at the server until I see it break. – user278859 Jul 05 '12 at 18:58
  • I am also facing the same poblem,can you share the code how to use unicode escape sequence – Akbar May 15 '13 at 05:49
  • this is a workaround here suggested in one of the later answers http://stackoverflow.com/questions/4901133/json-and-escaping-characters – Mousey Aug 17 '15 at 20:09
  • Print out the bytes that you receive, then check with the "Character Viewer" on your Mac that what you get is valid UTF-8. For example, if you have a stupid server that sends the data in Windows-1252, then this isn't going to work. (Not an iOS problem, I would hope that any Windows JSON parser would reject this as well). The degree character should be transmitted as C2 B0. B0 on its own is not valid UTF-8 and therefore not valid JSON. – gnasher729 Feb 09 '16 at 17:35

0 Answers0