5

I have a string "Artîsté". I use json_encode from PHP on it and I get "Art\u00eest\u00e9".

How do I convert that to an NSString? I have tried many things and none of them work I always end up getting Artîsté

For Example:
NSString stringWithUTF8String:"Art\u00c3\u00aest\u00c3\u00a9"];//Artîsté
@"Art\u00c3\u00aest\u00c3\u00a9"; //Artîsté
logancautrell
  • 8,762
  • 3
  • 39
  • 50
joels
  • 7,249
  • 11
  • 53
  • 94
  • But do you need them as literals or dynamically? Because literals can contain unescaped UTF without any kind of problem on OSX. – Jack Oct 25 '11 at 18:35
  • Dynamically. I make an NSURLConnection and the results that come back are the results of json_encode("Artîsté") which is "Art\u00c3\u00aest\u00c3\u00a9" – joels Oct 25 '11 at 18:48
  • Use a general-purpose JSON parser to decode JSON data including strings. NSStringEncodingNonLossyASCII is very similar to JSON string literal syntax but not 100% the same. – bobince Oct 25 '11 at 20:24

2 Answers2

7

You can use CFStringCreateFromExternalRepresentation with the kCFStringEncodingNonLossyASCII encoding to parse the \uXXXX escape sequences. Check out my answer here:

Converting escaped UTF8 characters back to their original form

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 1
    Good info thanks. I'm still getting the wrong info Artîsty©. I'm going to assume something is wrong on the php json_encode side since echo json_decode(json_encode("Artîsté")); gives me Artîsty© as well. Thanks. – joels Oct 25 '11 at 19:28
  • 1
    `"Art\u00eest\u00e9"` is correct JSON or Java string literal for `Artîsté`; `"Art\u00c3\u00aest\u00c3\u00a9"` isn't. You might also have a problem displaying the final results? – bobince Oct 25 '11 at 20:29
4

The problem is your input string:

"Art\u00c3\u00aest\u00c3\u00a9"

does in fact literally mean "Artîsté". \u00c3 is 'Ã', \u00ae is '®', and \u00a9 is '©'.

Whatever is producing your input string is receiving UTF-8 input but expecting something else (e.g., cp1252, ISO-8859-1, or ISO-8859-15)

bames53
  • 86,085
  • 15
  • 179
  • 244