iPhone 非拉丁字符编码

发布于 2024-10-17 19:09:35 字数 723 浏览 1 评论 0原文

我正在尝试解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光与爱终年不遇 2024-10-24 19:09:35

我认为有一个混乱。

π 是一个 HTML 实体,与 UTF8 / Latin 等文本编码无关。

阅读 wikipedia 了解有关...的详细信息。

您需要一个解析器来解码这些实体,如前面提到的那样作者:主要是伊兹:

NSString+HTML category and method stringByReplacingHTMLEntities

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:

NSString+HTML category and method stringByReplacingHTMLEntities
另类 2024-10-24 19:09:35

看看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.

长伴 2024-10-24 19:09:35

尝试使用这段代码:

  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

  NSString *decodedString = [NSString stringWithUTF8String:[responseString cStringUsingEncoding:[NSString defaultCStringEncoding]]];


  NSLog(@"response %@", decodedString);


  // array from the JSON string
  NSArray *results = [decodedString JSONValue];

Try to use this snippet of code:

  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

  NSString *decodedString = [NSString stringWithUTF8String:[responseString cStringUsingEncoding:[NSString defaultCStringEncoding]]];


  NSLog(@"response %@", decodedString);


  // array from the JSON string
  NSArray *results = [decodedString JSONValue];
醉生梦死 2024-10-24 19:09:35

我也遇到过同样的问题,但我通过更改 JSON 解析器解决了它。我已经开始使用 SBJSONParser,现在我得到了适当的结果。这是我用过的代码片段

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
SBJSON *parser=[[SBJSON alloc]init];
NSArray *JSONData = (NSArray*)[parser objectWithString:returnString error:nil];

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

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
SBJSON *parser=[[SBJSON alloc]init];
NSArray *JSONData = (NSArray*)[parser objectWithString:returnString error:nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文