2009-09-24 6 views
4

Ecriture d'une application iPhone, et j'obtiens mes données d'une API REST qui renvoie JSON. Mon problème est, certaines des données ont des caractères spéciaux incorporés et Im obtenant des erreurs. Exemple:iPhone, caractères spéciaux dans la réponse JSON

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

Voici mon code actuel:

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

Chaque fois que les données ont des caractères spéciaux dans ce "jsonString" return "(null)", sinon tout fonctionne bien.

Est-ce que c'est quelque chose que je peux gérer de mon côté, ou est-ce que l'API doit être modifiée pour afficher les codes de caractères?

Répondre

3

Si -initWithData:encoding: renvoie zéro, vos données ne sont presque certainement pas codées dans l'encodage demandé. Je suppose que vous n'envoyez pas de code UTF8, et que vous envoyez plutôt d'autres types d'encodage comme l'une des pages de code Windows ou Latin1. Voir String Encoding dans le NSString documentation.

2

Utilisation Les fonctions suivantes pour encoder votre code Je l'ai fait et travaille pour moi parfaitement ....

+ (NSData*) resolveXmlSpecialEntities:(NSData*)xmlData 
{ 
    if (xmlData == nil) 
     return nil; 
    if ([xmlData length] == 0) 
     return xmlData; 

    NSMutableString* xmlString = [[NSMutableString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding]; 
    [xmlString replaceOccurrencesOfString:@""" withString:@"\"" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"<" withString:@"##??##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@">" withString:@"##?!##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"<br>" withString:@"##br##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"</br>" withString:@"##/br##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"<b>" withString:@"##b##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"</b>" withString:@"##/b##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"<center>" withString:@"##center##"options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"</center>" withString:@"##/center##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"##??##![CDATA[" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"]]##?!##" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"&" withString:@"##ampersand##" options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 
    [xmlString replaceOccurrencesOfString:@"##ampersand##nbsp;" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [xmlString length])]; 

    xmlData = [xmlString dataUsingEncoding:NSUTF8StringEncoding]; 
    [xmlString release]; 
    return xmlData; 
} 

+ (NSString*) stringByRemovingResolversInXmlString:(NSString*)xmlString 
{ 
    NSMutableString* stringToChange = [NSMutableString stringWithString:xmlString]; 

    [stringToChange replaceOccurrencesOfString:@"##??##" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##?!##" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##br##" withString:@"<br>" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##/br##" withString:@"</br>" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##b##" withString:@"<b>" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##/b##" withString:@"</b>" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##center##" withString:@"<center>"options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##/center##" withString:@"</center>" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 
    [stringToChange replaceOccurrencesOfString:@"##ampersand##" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [stringToChange length])]; 

    return [NSString stringWithString:stringToChange]; 
} 
Questions connexes