0

This code shows correctly as Ecole with an accent on E:

NSString *test = @"\u00c9cole";
cell.detailTextLabel.text = test;

But when I get the string from my server sent as Json, I don't see E with an accent but rather the unicode \u00c9.

Code for getting Json string from server:

- (void) handleProfileDidDownload: (ASIHTTPRequest*) theRequest
{
    NSMutableString *str = [[NSMutableString alloc] init];
    [str setString:[theRequest responseString]];
    [self preprocess:str]; //NSLog here shows  str has the unicode characters \u00c9

}

- (void) preprocess: (NSMutableString*) str 
    {
    [str replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [str length])];
    [str replaceOccurrencesOfString:@"\"" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [str length])];
    [str replaceOccurrencesOfString:@"\\/" withString:@"/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [str length])];

}

Now if I do,

cell.detailTextLabel.text = str;

I don't get the accent for E rather \u00c9

What am I doing wrong?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
mbh
  • 3,302
  • 2
  • 22
  • 24

2 Answers2

1
NSString *test = @"\u00c9cole";

is converted by the complier to the accented E.

In your JSON, the string \u00c9cole is a literal backslash-u-zero-zero-c-nine.

You can get the same behavior by escaping the backslash.

NSString *test2 = @"\\u00c9cole";

This will give you the same bad result, \u00c9cole.


To correctly unescape the JSON string, see Using Objective C/Cocoa to unescape unicode characters, ie \u1234.

I'm providing the link instead of an answer because there are three decent answers. You can choose the best one for your needs.

Community
  • 1
  • 1
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0

NSLog here shows str has the unicode characters \u00c9

From that, you can know that the JSON you receive doesn't actually have the letter É inside but the escape sequence \u00c9. So, you need to somehow unescape this string:

CFMutableStringRef mutStr = (CFMutableStringRef)[str mutableCopy];
CFRange rng = { 0, [mutStr length] };
CFStringTransform(mutStr, &rng, CFSTR("Any-Hex/Java"), YES);

Then you can use mutStr in your code.