iPhone 非拉丁字符编码
我正在尝试解析 GET 请求的 JSON 响应。当字符是拉丁文时没问题。
但是,当它们不是拉丁语时,消息将无法正确显示。我尝试了希腊语,但不是“πανος”,而是“& pi; & alpha; & nu; & omicron; & sigmaf;”
我用于解析响应的代码是:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response %@", responseString);
// array from the JSON string
NSArray *results = [responseString JSONValue];
当我尝试使用 ajax 从网站读取响应时,一切都很好。当尝试使用来自 iphone 的数据向应用程序服务器发送 GET 请求时,同样适用。因此,当我将数据传输到服务器并从网站读取数据时,一切都很好。当我尝试在应用程序中显示相同的数据时,“休斯顿我们遇到了问题”。
有什么线索吗?
编辑:为了避免误解,这不是 HTML 的问题,我只是指出,对于某些 readon utf-8 字符,这里会正确且自动编码,例如。 “&pi”将被转换为“π”,但是 Objective c 似乎并没有自己做到这一点
I am trying to parse a JSON response of a GET request. When the characters, are latin no problem.
However when they are not latin the message doesn't come out correctly. I tried greek and instead of "πανος" i get "& pi; & alpha; & nu; & omicron; & sigmaf;"
The code I use for parsing the response is:
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response %@", responseString);
// array from the JSON string
NSArray *results = [responseString JSONValue];
When I try to read the response from a website using ajax, everything is fine. The same applies when trying to send a GET request to the application servers with data from iphone. So when i transmit data to the server and read it from the website everything is fine. When i try to show the same data in the app, "Houston we have a problem".
Any clues?
EDIT: To avoid misunderstandings, it's not an issue of HTML, I just point out that for some readon utf-8 characters here are encoded correctly and automatically eg. "&pi" will be converted to "π", however objective c doesn't seem to do this on its own
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为有一个混乱。
π
是一个 HTML 实体,与 UTF8 / Latin 等文本编码无关。阅读 wikipedia 了解有关...的详细信息。
您需要一个解析器来解码这些实体,如前面提到的那样作者:主要是伊兹:
There is a confusion I think.
π
is an HTML entity which is unrelated to text encoding like UTF8 / Latin.Read wikipedia for details about...
You need a parser to decode these entities like the one previously mentioned by Chiefly Izzy:
看看Cocoanetics NSString+HTML 类的方法和stringByReplacingHTMLEntities 方法。您可以在以下位置找到它:
https:/ /github.com/Cocoanetics/NSAttributedString-Additions-for-HTML/blob/master/Classes/NSString%2BHTML.m
这是一个相当不错的 HTML 实体及其相应的 unicode 字符的列表。
Look at Cocoanetics NSString+HTML category and method stringByReplacingHTMLEntities method. You can find it at:
https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML/blob/master/Classes/NSString%2BHTML.m
Here's a pretty decent list of lot of HTML entities and their corresponding unicode characters.
尝试使用这段代码:
Try to use this snippet of code:
我也遇到过同样的问题,但我通过更改 JSON 解析器解决了它。我已经开始使用 SBJSONParser,现在我得到了适当的结果。这是我用过的代码片段
I have faced the same problem, but I solved it by changing the JSON parser. I have started using the SBJSONParser, and now I am getting the appropriate results. This is the code snippet, I have used