JSONObjectWithData returns null if degree symbol is in json object

♀尐吖头ヾ 提交于 2019-12-14 03:57:34

问题


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

来源:https://stackoverflow.com/questions/11349819/jsonobjectwithdata-returns-null-if-degree-symbol-is-in-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!