我有一个应用程序支持四种语言。在那个时候,当我用中文用户名登录时,它会显示我这样的响应..
[{"0":"41","intid":"41","1":"\u8a00\u3046","varfirstname":"\u8a00\u3046","2":"\u8a00\u3046","varlastname":"\u8a00\u3046","3":"\u5730","varusername":"\u5730","4":"[email protected]","varemailid":"[email protected]","5":"qwert","varpassword":"qwert","6":"12345","varmobileno":"12345","7":"Enable","mobileMessage":"Enable","8":"","varphoneno":"","9":"Enable","enumstatus":"Enable","10":"2013-01-30","date_insert":"2013-01-30","11":"2013-01-30","date_edit":"2013-01-30","12":"1.38.28.36","varipaddress":"1.38.28.36"}]
我想向 UITextfield Text 显示 "varfirstname" 。但是当我在 NSLog 中打印它时,我没有得到任何文本。
NSLog(@"Text is === %@",textfname,text);
如何解码此文本?并在 UITextfield 或 UILabel 上显示。
Best Answer-推荐答案 strong>
我刚刚搜索了一下,发现了来自 here 的有用答案之一.
中文和日文字符自然不适用于 ASCII 字符串编码。如果您尝试通过 Apple 的方法转义字符串,您绝对应该避免代码重复,将结果存储为 Unicode 字符串。使用以下编码之一:
NSUTF8StringEncoding
NSUTF16StringEncoding
NSShiftJISStringEncoding (not Unicode, Japanese-specific)
更新
例如,您可以像下面这样编码解码您的中文字符串:
NSString * test = @"汉字马拉松是";
NSString* encodedString =[test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"====%@",encodedString);
输出是:
%E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF
然后像这样解码:
NSString* originalString =[encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"====%@",originalString);
输出是:
汉字马拉松是
关于ios - 中文字符编码问题ios,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/14619316/
|