1

我试图在 Stackoverflow 中搜索。但我无法得到正确的解决方案。有人可以帮帮我吗?

我有来自网络服务的数据。

KEY: 1036   TYPE: string    VALUE = French (fran\u00E7aise)
KEY: 1032   TYPE: string    VALUE = Greek  (\u03B5\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AC)

有谁知道如何正确打印这个 Unicode 字符串?我试图对其进行编码和解码(如下所示)。但它不起作用。:(

NSString *value = [NSString stringWithFormat:@"%@", t.dataValue]; 
NSData *bytes = [message dataUsingEncoding:NSUTF8StringEncoding];
NSString* messageDecoded = [[NSString alloc] initWithData:bytes encoding:NSUTF8StringEncoding];
NSLog(@"decoded: %@", messageDecoded);

提前谢谢各位!

回答:

我从这个链接得到了答案:

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);
4

0 回答 0