问题
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