编码问题:Cocoa错误261? [英] Encoding issue: Cocoa Error 261?

查看:100
本文介绍了编码问题:Cocoa错误261?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从我的iPhone应用程序的php脚本中获取JSON字符串使用:

So I'm fetching a JSON string from a php script in my iPhone app using:

NSURL *baseURL = [NSURL URLWithString:@"test.php"];
NSError *encodeError = [[NSError alloc] init];
NSString *jsonString = [NSString stringWithContentsOfURL:baseURL encoding:NSUTF8StringEncoding error:&encodeError];
NSLog(@"Error: %@", [encodeError localizedDescription]);
NSLog(@"STRING: %@", jsonString);

JSON字符串在我测试输出时验证。现在我有一个编码问题。当我获取单个echo'd行时,例如:

The JSON string validates when I test the output. Now I'm having an encoding issue. When I fetch a single echo'd line such as:

{ "testKey":"é" }

JSON解析器运行正常,我可以创建一个有效的JSON对象。但是,当我获取我的2MB JSON字符串,我得到:

The JSON parser works fine and I am able to create a valid JSON object. However, when I fetch my 2MB JSON string, I get presented with:

Error: Operation could not be completed. (Cocoa error 261.)

和一个Null字符串。我的PHP文件是UTF8本身,我不使用utf8_encode(),因为这似乎双重编码数据,因为我已经拉数据作为NSUTF8StringEncoding。无论哪种方式,在我的单回声测试中,这是允许我在构建JSON对象时成功记录\ASDAS风格的UTF8转义的方法。

and a Null string. My PHP file is UTF8 itself and I am not using utf8_encode() because that seems to double encode the data since I'm already pulling the data as NSUTF8StringEncoding. Either way, in my single-echo test, it's the approach that allowed me to successfully log \ASDAS style UTF8 escapes when building the JSON object.

在更大的字符串的情况下的错误?

What could be causing the error in the case of the larger string?

此外,我不知道是否有所作为,但我使用php函数addslashes

Also, I'm not sure if it makes a difference, but I'm using the php function addslashes() on my parsed php data to account for quotes and such when building the JSON string.

推荐答案

不知道这是不是你的问题,但我只是有一个类似的东西(stringWithContentsOfFile,没有JSON),问题是,该文件有CRLF(窗口)行结束和西方 - 无论它被称为编码。我使用SubEthaEdit转换为LF(Mac / Unix行尾)和UTF-8编码,现在一切正常。

Don't know if this is your problem, but I just had a similar thing (stringWithContentsOfFile, no JSON), and the problem was that the file had CRLF (windows) line-endings and Western-whatever-it's-called encoding. I used SubEthaEdit to convert to LF (Mac/Unix line-endings) and UTF-8 encoding, and now everything works fine.

这篇关于编码问题:Cocoa错误261?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆