json Objective C中的UTF8字符解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4913499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UTF8 character decoding in Objective C
提问by Gaurav Ghate
I am trying to parse a feed from a json webservice on my iPhone however the utf8 conversion is not working the way it should or am I doing something wrong
Here is apart of the feed est un r\u00c3\u00aave en ntheitroad
我正在尝试从 iPhone 上的 json 网络服务解析提要,但是 utf8 转换没有按照应有的方式工作,或者我做错了什么这里是提要的一部分 est un r\u00c3\u00aave en ntheitroad
Here is the code I have written to convert the above:
这是我编写的用于转换上述内容的代码:
NSString* str1 = @"est un r \u00c3\u00aa ve en ntheitroad";
NSString* str = [NSString stringWithUTF8String:[str1 cStringUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Converted UTF %@",str);
This is the output that I am getting:Converted UTF est un r ?a ve en ntheitroad
这是我得到的输出:Converted UTF est un r ?a ve en ntheitroad
The expected output should beConverted UTF est un rêve en ntheitroad
预期的输出应该是Converted UTF est un rêve en ntheitroad
When I checked the UTF table and used a utf converter online the output is correctly got with the same code and using the string @"est un r\u00EAve en ntheitroad"but I think that the UTF16 Representation if I am not mistaken. Now am really in a fix how to parse this feed.
当我检查 UTF 表并在线使用 utf 转换器时,输出是使用相同的代码和字符串正确获得的,@"est un r\u00EAve en ntheitroad"但如果我没记错的话,我认为是 UTF16 表示。现在我真的在解决如何解析这个提要。
回答by яοвοτа?τ?яа??
After much grief I have figured this out. Use this.
在经历了很多悲痛之后,我明白了这一点。用这个。
NSString *correctString = [NSString stringWithCString:[utf8String cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
回答by Tommy
I think the test case is broken; the following:
我认为测试用例坏了;下列:
NSString* str1 = @"est un r \u00c3\u00aa ve en ntheitroad";
NSLog(@"%@", str1);
Also outputs 'est un r ?a ve en ntheitroad'. However, this:
还输出“est un r ?a ve en ntheitroad”。然而,这:
NSString* str1 = @"est un rêve en ntheitroad";
NSLog(@"%@", str1);
Outputs 'est un rêve en ntheitroad', as does:
输出 'est un rêve en ntheitroad',如下所示:
NSString* str1 = @"est un rêve en ntheitroad";
NSString* str = [NSString stringWithUTF8String:[str1 cStringUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@", str);
And ditto for the slightly shorter version:
对于稍短的版本也是如此:
NSString* str1 = @"est un rêve en ntheitroad";
NSString* str = [NSString stringWithUTF8String:[str1 UTF8Encoding]];
NSLog(@"%@", str);
And, indeed:
而且,确实:
char *str1 = "est un r\xc3\xaave en ntheitroad";
NSString* str = [NSString stringWithUTF8String:str1];
NSLog(@"%@", str);
I think it's a question of JSON, not UTF8. The \u followed by four hexadecimal digits is JSON's way of encoding a generic UTF16 character, it's not an inherent part of UTF. So NSString doesn't know how to deal with it. Your JSON parser needs to be adapted to parse escape sequences properly.
我认为这是 JSON 的问题,而不是 UTF8。\u 后跟四个十六进制数字是 JSON 编码通用 UTF16 字符的方式,它不是 UTF 的固有部分。所以 NSString 不知道如何处理它。您的 JSON 解析器需要进行调整以正确解析转义序列。
回答by dip
In iOS 9 don't need do anything. It is converted automatically.
在 iOS 9 中不需要做任何事情。它是自动转换的。
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:postData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"response header: %@",response);
NSString* aStr;
aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response data: %@", aStr);
NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSDictionary *dict in json)
{
NSLog(@"%@", dict);
NSString *name = [dict objectForKey:@"name"];
NSString *msg = [dict objectForKey:@"message"];
NSString *topic = [dict objectForKey:@"topic"];
NSLog(@"*********** %@, %@, %@", name,topic,msg);
// now do something
}
回答by Sandeep Khade
While sending to server
发送到服务器时
NSString* str1 = @"est un r \u00c3\u00aa ve en ntheitroad";
NSData *str1Data = [str1 dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *str2 = [[NSString alloc] initWithData:str1Data encoding:NSUTF8StringEncoding];
After receiving you can decode like this
收到后可以这样解码
NSData *recivedStrData = [recivedStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *strResult = [[NSString alloc] initWithData:recivedStrData encoding:NSNonLossyASCIIStringEncoding];
This works for me!
这对我有用!

