views:

404

answers:

3

Writing an iphone app, and I'm getting my data from a REST API that returns JSON. My problem is, some of the data has special characters embedded and Im getting errors. Example:

MartÌn
Petite-RiviËre-Saint-FranÁois

Here is my current code:

NSString *jsonString = [[NSString alloc] 
                            initWithData:receivedData 
                            encoding:NSUTF8StringEncoding];
NSMutableArray *tempResults = [[[jsonString JSONValue] 
                            objectForKey:@"getRegionResortsLastUpdatedResponse"] 
                            objectForKey:@"return"];

Whenever the data has special characters in it "jsonString" return "(null)", otherwise everything works fine.

Is this something I can handle on my end, or does the API need to be modified to output the character codes?

+2  A: 

If -initWithData:encoding: returns nil, your data is almost certainly not encoded in the requested encoding. I suspect you're not sending UTF8, and are rather sending some other encoding such as one of the Windows code pages or Latin1. See String Encoding in the NSString documentation.

Rob Napier
A: 

smacks self on forehead

That did it, thanks!

eddit
A: 

i have a similar problem...however, for me it is not a question of nil. Instead, i get results like "Liu00e8ge" instead of "Liège"...my json header is configured to render utf8, as is the DB (DEFAULT CHARSET=utf8). In HTML, these characters render fine, but in my UILabel, i get the utf8 code. Here is how i retreive my data:

NSString *feedURL = @"http://site.local/places/view/740";
NSLog(@"---------> SENDING QUERRY : %@",feedURL);

NSData * contentsData = [NSData dataWithContentsOfURL:[NSURL URLWithString:feedURL]];
NSString *contents;
if (contentsData) 
{
    contents = [[NSString alloc] initWithData: contentsData encoding:NSUTF8StringEncoding];
}
NSLog(@"contents %@",contents);//funny characters still there
NSDictionary *dict = [NSDictionary dictionaryWithDictionary:[contents JSONValue]];
mylabel.text = [dict valueForKeyPath:@"Place.name"];

any ideas? i have a lot of these characters!

thanks

mlecho
ohhhh man...this was a bad on the backend...we had stripslashes, which was taking out the \ before \u[CODE]... well, thought i would throw it out there, in case there are others
mlecho