4

I am writing an app with both english and french support. The app requests information from a server and the server response uses JSON. I am using the JSONKit library to parse the response but the strings its parsing from the french responses look like this:

Membres –Économisez 5% sur les services et jusqu’à 15% sur d’autres produits

How do I decode the special characters? so that I get this:

Membres –Économisez 5% sur les services et jusqu’à 15% sur d’autres produits

I looked at the API for NSString and tried some of the instance methods but I don't know much about character encodings and I ended up getting some weird results. So if you can also provide a brief explanation on character encodings I'd really appreciate it.

Thanks in advance

luis.ramireznossa
  • 261
  • 1
  • 2
  • 7
  • Possible duplicate of: http://stackoverflow.com/questions/1105169/html-character-decoding-in-objective-c-cocoa-touch – mja Sep 22 '11 at 14:51
  • Possible duplicate of http://stackoverflow.com/questions/7513312/response-encryption-problem/7514003#7514003 – zaph Sep 22 '11 at 15:31

3 Answers3

4

This solution worked well for me if you're still using Objective C code.

- (NSString *)decodeHtmlStringValue
{
  NSData *encodedString = [self dataUsingEncoding:NSUTF8StringEncoding];
  NSAttributedString *htmlString = [[NSAttributedString alloc] initWithData:encodedString
                                                                    options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];
  return [htmlString string];
}

If your using swift

https://stackoverflow.com/questions/25607247/how-do-i-decode-html-entities-in-swift

StackRunner
  • 1,463
  • 2
  • 16
  • 22
3

Check out these NSString categories

Alastair Stuart
  • 4,165
  • 3
  • 36
  • 33
  • How would I go about using this? Probably a dumb question but I'm still fairly new to iPhone programming – luis.ramireznossa Sep 22 '11 at 17:24
  • Add that file and its matching h header file to your project. NSString will then have a new instance method called stringByDecodingHTMLEntities. NSString *decodedString = [encodedString stringByDecodingHTMLEntities]; – Alastair Stuart Sep 25 '11 at 14:43
1

NSString stringByReplacingPercentEscapesUsingEncoding with the correct string encoding should do the magic.

[yourString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

might be a good candidate.

cellcortex
  • 3,166
  • 1
  • 24
  • 34
  • 1
    I tried: NSString *myString = [myOtherString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; but its returning null. I checked myOtherString and it contains the encoded string. – luis.ramireznossa Sep 22 '11 at 17:45
  • The documentation states: Returns nil if the transformation is not possible, for example, the percent escapes give a byte sequence not legal in encoding. – Martin Winter Apr 23 '14 at 13:56
  • 1
    This is for percent decoding, the question is asking for html code decoding – miracle-doh Nov 01 '16 at 17:00