Question

I am making a call to get a JSON response like this:

NSData *urlData=[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&httpResponse error:nil ];
NSString *returnString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

However, when I print the string using NSLog:

Emiratos �rabes Unidos

When I convert it to NSData like this:

NSData *jsonData = [returnString dataUsingEncoding:NSUTF8StringEncoding];
NSArray * response = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

It turns it to be (when I retrieve the value from the array):

Emiratos \Ufffdrabes Unidos

And when I put it in a label it displays it like this:

Emiratos �rabes Unidos

I would like to display in a label like this:

Emiratos Árabes Unidos 

How can I do it?

Was it helpful?

Solution

The problem seems to be this line:

NSString *returnString = 
    [[NSString alloc] initWithData:urlData 
                          encoding:NSUTF8StringEncoding];

You are assuming that the data is a string encoded as UTF8. But apparently it isn't. Therefore you're seeing the "replacement character" (codepoint U+FFFD) at this point.

You'll need to find out what encoding is actually being used. You can probably just experiment with other encodings. Alternatively, use NSLog to look at the data; an NSData object is logged as a sequence of hex bytes, so by looking at the bytes in that position, and by looking up various encodings on the Internet, you may be able to deduce what encoding is being used here.

(But if you use NSLog and you actually see FFFD at this point, then you've had it; the server itself is supplying the bad data and there's nothing you can do about it, as the good data is lost before you can get at it.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top