Zebs
Zebs

Reputation: 5438

NSString encoding for accents displays crazy characters

I am downloading data (text) from a server.

I have tried with both:NSISOLatin1StringEncoding and NSASCIIStringEncoding

But I keep seeing things like: {"estado":"M\u00e9xico"}

Noting that it should read México and not M\u00e9xico (with an accent over the e).

Looking online I figured that \u00e9 is in fact é link.

But the NSString is not able to interpret this and instead prints weird things on my UILabels:

enter image description here

I would really really appreciate your help on this.

Alsso, if you are itnerested, you can download the data from here: http://www.miorden.com/demo/iphone/estadoJSON.php

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"]];

NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

NSLog(@"Downloaded: %@", string);    

string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"] encoding:NSISOLatin1StringEncoding error:nil];

NSLog(@"Downloaded: %@", string);

I have been literally trying for days and it is killing me!

Thank you so much!

Upvotes: 2

Views: 1877

Answers (2)

Stefan
Stefan

Reputation: 1496

The data is in JSON format, so you'll need to JSON decode it too.

For example using this: https://github.com/TouchCode/TouchJSON

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"]];

NSError *error;
NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:data error:&error];

NSLog(@"Test: %@", [[array objectAtIndex:11] valueForKey:@"estado"]);

outputs

2011-08-11 09:35:45.742 enctest[63236:407] Test: México

Upvotes: 1

Mitchell Currie
Mitchell Currie

Reputation: 2809

That appears to be unicode, try NSUTF8StringEncoding.

Upvotes: 3

Related Questions