经常在调试服务端返回 JSON 结果的时候遇到Unicode 编码的表示方法:
{
content ="\U6c5f\U897f\U7701\U4e0a\U5883\U946b\U4fce\U65e5\U4e2d\U5927\U9058155\U53f71\U5e62304\U5792";**
desc = "\U4f4f\U5740";
index = "<null>";
nID = "<null>";
}
从网络上获得的unicode码的开头字母是大写U,一定要先转换成小写。
- (NSString *)transformDic:(NSDictionary *)dic {
if (![dic count]) {
return nil;
}
NSString *tempStr1 =
[[dic description] stringByReplacingOccurrencesOfString:@"\u"
withString:@"\U"];
NSString *tempStr2 =
[tempStr1 stringByReplacingOccurrencesOfString:@""" withString:@"\""];
NSString *tempStr3 =
[[@""" stringByAppendingString:tempStr2] stringByAppendingString:@"""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:NULL error:NULL];
return str;
}
方法的调用:
//responseObject是接口返回来的Unicode数据
NSLog(@" %@",[self transformDic:responseObject]);
网友评论