Igor Camilo Igor Camilo - 1 year ago 102
JSON Question

NSJSONSerialization JSONObjectWithData:options:error: encoding issue

In my code, I download a

JSON
data encoded in
UTF8
, which is the ideal to work with
NSJSONSerialization
, but when I get the parsed object, it's full of encoding problems such as:


"Jo\U00e3o e Maria - Ca\U00e7adores de Bruxas"


(it must be "João e Maria - Caçadores de Bruxas"). Tried to reencode it in ASCII, Unicode and others with no success:

NSData *downloadedData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:requestURL cachePolicy:cachePolicy timeoutInterval:timeoutInterval] returningResponse:NULL error:&error];
NSDictionary *serializedDictionary = [NSJSONSerialization JSONObjectWithData:downloadedData options:NSJSONReadingAllowFragments error:&error];


EDIT: When I print using this, it shows correctly:

NSLog(@"Test: %@", [[NSString alloc] initWithData:downloadedData encoding:NSUTF8StringEncoding]);

Answer Source

Those are UTF8 encoded characters. The format is correct, put them on a UILabel or something and they'll appear as you expect.