当我尝试将NSData从请求转换为NSDictionary对象时,我使用NSURLSession收到此错误:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7f83f6ba11b0 {NSDebugDescription=Invalid value around character 0.}
我已经记录了从我的PHP后端发送的JSON,它看起来像这样:
[{"comment":"refresh2","something":"Bob"},{"comment":"refresh","something":"Bob"},{"comment":"show","something":"Bob"},{"comment":"huh","something":"Bob"},{"comment":"erase replace","something":"Bob"},{"comment":"will it add this one?","something":"Bob"},{"comment":"a comment to add","something":"Bob"},{"comment":"upload","something":"Bob"},{"comment":"work","something":"Bob"},{"comment":"add","something":"Bob"},{"comment":"will it work?","something":"Bob"},{"comment":"did this add?","something":"Bob"},{"comment":"another comment","something":"Bob"},{"comment":"storycomment","something":"Bob"}]
我检查了jsonlint以确保它是有效的JSON ......它是。
以下是产生错误的代码:
NSError *jsonError;
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&jsonError];
我非常困惑,因为我知道JSON是有效的。我过去为解决此错误而做的一件事是在php文件中注释掉我发送请求的所有echo
和print/print_r
语句。但是,php文件中没有echo
或print
语句......我仍然会收到此错误 - 非常感谢任何帮助。
更新
我按照发现here的说明寻找不需要的字符......虽然我不知道在输出中要查找什么...如果有人可以查看这些并告诉我他们是否是否涉及任何可能导致我得到的错误的字符会很棒。我不得不缩短产量 - 这一切都不适合这个问题。
2015-01-27 14:14:47.249 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.249 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.250 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.250 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=/ hex=2f
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.251 spiritswitch[813:15915] char=
hex=a
2015-01-27 14:14:47.252 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.252 spiritswitch[813:15915] char=f hex=66
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.253 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.254 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=z hex=7a
2015-01-27 14:14:47.254 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.255 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.255 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=< hex=3c
2015-01-27 14:14:47.256 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.257 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.258 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.258 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.259 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.259 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=x hex=78
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.260 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=u hex=75
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=- hex=2d
2015-01-27 14:14:47.261 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.262 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.263 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=x hex=78
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.263 spiritswitch[813:15915] char=- hex=2d
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.264 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.265 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.265 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.265 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=t hex=74
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.266 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.266 spiritswitch[813:15915] char=b hex=62
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=o hex=6f
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.267 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=r hex=72
2015-01-27 14:14:47.268 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.268 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.269 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.269 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.270 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=s hex=73
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=p hex=70
2015-01-27 14:14:47.271 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.272 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.273 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.274 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.274 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.274 spiritswitch[813:15915] char=0 hex=30
2015-01-27 14:14:47.341 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.342 spiritswitch[813:15915] char= hex=20
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=c hex=63
2015-01-27 14:14:47.342 spiritswitch[813:15915] char=e hex=65
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=l hex=6c
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=p hex=70
2015-01-27 14:14:47.343 spiritswitch[813:15915] char=a hex=61
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=d hex=64
2015-01-27 14:14:47.344 spiritswitch[813:15915] char=i hex=69
2015-01-27 14:14:47.345 spiritswitch[813:15915] char=n hex=6e
2015-01-27 14:14:47.345 spiritswitch[813:15915] char=g hex=67
2015-01-27 14:14:47.345 spiritswitch[813:15915] char== hex=3d
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=1 hex=31
2015-01-27 14:14:47.346 spiritswitch[813:15915] char=' hex=27
2015-01-27 14:14:47.347 spiritswitch[813:15915] char=> hex=3e
2015-01-27 14:14:47.347 spiritswitch[813:15915] char=
hex=a
更新
以下是我提出请求时处理响应的PHP
代码 - 我被告知我应该将Content-Type
更改为application/json
,但我已经尝试过这样做了#39; t help - 我会再试一次 - 但如果你看到这段代码有问题,请告诉我:
function sendResponse($status=null, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
print $body;
}
答案 0 :(得分:4)
这可能听起来很愚蠢 - 但你确定要问的是正确的网址吗?请看我的其他帖子,我遇到了同样的问题。 https://stackoverflow.com/a/28393273/557362
首先从URL响应开始,看看到底发生了什么。
您可以使用NSLog [NSString stringWithUTF8String:[data bytes]]
来获取不错的数据版本,但不要忘记查看HTTP响应。
记录[response description]
。