iPhone encoding of non latin characters

1.1k views Asked by At

I am trying to parse a JSON response of a GET request. When the characters, are latin no problem.

However when they are not latin the message doesn't come out correctly. I tried greek and instead of "πανος" i get "& pi; & alpha; & nu; & omicron; & sigmaf;"

The code I use for parsing the response is:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"response %@", responseString);




// array from the JSON string
NSArray *results = [responseString JSONValue];

When I try to read the response from a website using ajax, everything is fine. The same applies when trying to send a GET request to the application servers with data from iphone. So when i transmit data to the server and read it from the website everything is fine. When i try to show the same data in the app, "Houston we have a problem".

Any clues?

EDIT: To avoid misunderstandings, it's not an issue of HTML, I just point out that for some readon utf-8 characters here are encoded correctly and automatically eg. "&pi" will be converted to "π", however objective c doesn't seem to do this on its own

4

There are 4 answers

0
Vincent Guerci On BEST ANSWER

There is a confusion I think.

π is an HTML entity which is unrelated to text encoding like UTF8 / Latin.

Read wikipedia for details about...

You need a parser to decode these entities like the one previously mentioned by Chiefly Izzy:

NSString+HTML category and method stringByReplacingHTMLEntities
0
iHS On

I have faced the same problem, but I solved it by changing the JSON parser. I have started using the SBJSONParser, and now I am getting the appropriate results. This is the code snippet, I have used

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
SBJSON *parser=[[SBJSON alloc]init];
NSArray *JSONData = (NSArray*)[parser objectWithString:returnString error:nil];
1
zrzka On

Look at Cocoanetics NSString+HTML category and method stringByReplacingHTMLEntities method. You can find it at:

https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML/blob/master/Classes/NSString%2BHTML.m

Here's a pretty decent list of lot of HTML entities and their corresponding unicode characters.

0
Matteo Alessani On

Try to use this snippet of code:

  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

  NSString *decodedString = [NSString stringWithUTF8String:[responseString cStringUsingEncoding:[NSString defaultCStringEncoding]]];


  NSLog(@"response %@", decodedString);


  // array from the JSON string
  NSArray *results = [decodedString JSONValue];