2

I am fetching data from URL, and this data contains special characters, such as in the name Désirée.

I want to display this in my TableView cell, but when I display it, it looks like this: Dösiröe.

How do I make it display correctly?

Fetch data

NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URL] encoding: NSStringEncodingConversionAllowLossy error:nil];
SBJSON *parser = [[SBJSON alloc] init];
dictShow = [parser objectWithString:jsonString error:nil];
arr=[dictShow copy];

Display Data in TableView

cell.textLabel.text = [arr objectAtIndex:indexPath.row];
John Hascall
  • 9,176
  • 6
  • 48
  • 72
Krunal
  • 6,440
  • 21
  • 91
  • 155
  • 1
    How are you fetching your data? You need to interpret it using `NSUTF8StringEncoding` rather than an old ISO encoding. – Cyrille Apr 23 '13 at 09:51
  • You have an encoding problem somewhere. It could be server side or client-side. Post the code where you download the data. – Mike Weller Apr 23 '13 at 09:51
  • I am using json for fetching the data and storing it in a Dictionary.. – Krunal Apr 23 '13 at 09:52
  • Have you inserted that code..? - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; } – aBilal17 Apr 23 '13 at 09:53
  • NSStringEncodingConversionAllowLossy is not an encoding. It is a conversion option, and the value happens to be the same as NSASCIIStringEncoding. Use NSUTF8StringEncoding instead as Cyrille already suggested. – Martin R Apr 23 '13 at 09:59
  • Good suggestion it is working..i am able to display it in tableview but i am unable to fetch data when i pass `Désirée abcd` to my new URL data is there but i am unable to fetch it.. i have used same code for fetching the data which i wrote above. – Krunal Apr 23 '13 at 10:04
  • @Krunal: Why did you edit your question long after the answer has been accepted? That will it make impossible for future readers to understand the answer. – Martin R May 17 '13 at 06:01

1 Answers1

6

just use NSUTF8StringEncoding instead of NSStringEncodingConversionAllowLossy like....

NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:URL] encoding:NSStringEncodingConversionAllowLossy error:nil];
Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76
  • Yes, `NSStringEncodingConversionAllowLossy` is not actually a valid encoding type. It's an option to control how characters should be handled when *en*coding a string – Mike Abdullah Apr 25 '13 at 13:24