UTF-8 conversion UTF-8 conversion json json

UTF-8 conversion


You can use an approach based on the NSScanner. The following code (not bug-proof) can gives you a way on how it can work:

NSString *source = [NSString stringWithString:@"Le pass\\u00e9 compos\\u00e9 a \\u00e9t\\u00e9 d\\u00e9compos\\u00e9."];NSLog(@"source=%@", source);NSMutableString *result = [[NSMutableString alloc] init];NSScanner *scanner = [NSScanner scannerWithString:source];[scanner setCharactersToBeSkipped:nil];while (![scanner isAtEnd]) {    NSString *chunk;    // Scan up to the Unicode marker    [scanner scanUpToString:@"\\u" intoString:&chunk];    // Append the chunk read    [result appendString:chunk];    // Skip the Unicode marker    if ([scanner scanString:@"\\u" intoString:nil]) {        // Read the Unicode value (assume they are hexa and four)        unsigned int value;        NSRange range = NSMakeRange([scanner scanLocation], 4);        NSString *code = [source substringWithRange:range];        [[NSScanner scannerWithString:code] scanHexInt:&value];        unichar c = (unichar) value;        // Append the character        [result appendFormat:@"%C", c];        // Move the scanner past the Unicode value        [scanner scanString:code intoString:nil];    }}NSLog(@"result=%@", result);


If you use the JSON Framework, then all you do is get your JSON string and convert it to an NSArray like so:

NSString * aJSONString = ...;NSArray * array = [aJSONString JSONValue];

The library is well-written, and will automatically handle UTF8 encoding, so you don't need to do anything beyond this. I've used this library several times in apps that are on the store. I highly recommend using this approach.