编写iphone应用程序,我从返回JSON的REST API获取数据。我的问题是,一些数据嵌入了特殊字符,我得到错误。例如:
MartÌn
Petite-RiviËre-Saint-FranÁois
这是我目前的代码:
NSString *jsonString = [[NSString alloc]
initWithData:receivedData
encoding:NSUTF8StringEncoding];
NSMutableArray *tempResults = [[[jsonString JSONValue]
objectForKey:@"getRegionResortsLastUpdatedResponse"]
objectForKey:@"return"];
每当数据中有特殊字符时,“jsonString”返回“(null)”,否则一切正常。
这是我可以处理的事情,还是需要修改API以输出字符代码?
答案 0 :(得分:3)
如果-initWithData:encoding:
返回nil,则您的数据几乎肯定不会按请求的编码进行编码。我怀疑你没有发送UTF8,而是发送一些其他编码,例如Windows代码页或Latin1之一。请参阅NSString documentation。
答案 1 :(得分:2)
使用以下函数对我执行此操作的代码进行编码,并且完美地为我工作....
+ (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];
}