问题描述
我从中获取数据的某些站点返回 UTF-8 字符串,其中转义了 UTF-8 字符,即:u5404u500bu90fd
Some sites that I am fetching data from are returning UTF-8 strings, with the UTF-8 characters escaped, ie: u5404u500bu90fd
是否有内置的可可函数可以帮助解决这个问题,或者我是否必须编写自己的解码算法.
Is there a built in cocoa function that might assist with this or will I have to write my own decoding algorithm.
推荐答案
没有内置函数来做 C 转义.
There is no built-in function to do C unescaping.
您可以使用 NSPropertyListSerialization
作弊,因为旧文本样式"plist 支持通过 Uxxxx
进行 C 转义:
You can cheat a little with NSPropertyListSerialization
since an "old text style" plist supports C escaping via Uxxxx
:
NSString* input = @"ab"cA"BC\u2345\u0123";
// will cause trouble if you have "abc\\uvw"
NSString* esc1 = [input stringByReplacingOccurrencesOfString:@"\u" withString:@"\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:@""" withString:@"\""];
NSString* quoted = [[@""" stringByAppendingString:esc2] stringByAppendingString:@"""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListFromData:data
mutabilityOption:NSPropertyListImmutable format:NULL
errorDescription:NULL];
assert([unesc isKindOfClass:[NSString class]]);
NSLog(@"Output = %@", unesc);
但请注意,这不是很有效.如果您编写自己的解析器,那就更好了.(顺便说一句,您在解码 JSON 字符串吗?如果是,您可以使用 现有的 JSON 解析器.)
but mind that this isn't very efficient. It's far better if you write up your own parser. (BTW are you decoding JSON strings? If yes you could use the existing JSON parsers.)
这篇关于使用 Objective C/Cocoa 对 unicode 字符进行转义,即 u1234的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!