NSString 重音编码显示疯狂的字符
我正在从服务器下载数据(文本)。
我尝试过使用: NSISOLatin1StringEncoding
和 NSASCIIStringEncoding
但我不断看到类似的内容: {"estado":"M\u00e9xico"}
注意到它应该读作 México
而不是 M\u00e9xico
(在 e 上加重音)。
上网查了一下,我发现 \u00e9
实际上是 é
链接。
但是 NSString 无法解释这一点,而是在我的 UILabels 上打印奇怪的东西:
我非常感谢您对此的帮助。
另外,如果您有兴趣,可以从这里下载数据: http://www .miorden.com/demo/iphone/estadoJSON.php
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"Downloaded: %@", string);
string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"] encoding:NSISOLatin1StringEncoding error:nil];
NSLog(@"Downloaded: %@", string);
我已经尝试了好几天了,这简直要了我的命!
太感谢了!
I am downloading data (text) from a server.
I have tried with both:NSISOLatin1StringEncoding
and NSASCIIStringEncoding
But I keep seeing things like: {"estado":"M\u00e9xico"}
Noting that it should read México
and not M\u00e9xico
(with an accent over the e).
Looking online I figured that \u00e9
is in fact é
link.
But the NSString is not able to interpret this and instead prints weird things on my UILabels:
I would really really appreciate your help on this.
Alsso, if you are itnerested, you can download the data from here: http://www.miorden.com/demo/iphone/estadoJSON.php
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"Downloaded: %@", string);
string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.miorden.com/demo/iphone/estadoJSON.php"] encoding:NSISOLatin1StringEncoding error:nil];
NSLog(@"Downloaded: %@", string);
I have been literally trying for days and it is killing me!
Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是 unicode,尝试 NSUTF8StringEncoding。
That appears to be unicode, try NSUTF8StringEncoding.
数据是 JSON 格式,因此您也需要对其进行 JSON 解码。
例如使用这个: https://github.com/TouchCode/TouchJSON
输出
The data is in JSON format, so you'll need to JSON decode it too.
For example using this: https://github.com/TouchCode/TouchJSON
outputs