NSDebugDescription=字符 0 周围的值无效。- JSON 错误
Posted
技术标签:
【中文标题】NSDebugDescription=字符 0 周围的值无效。- JSON 错误【英文标题】:NSDebugDescription=Invalid value around character 0. - JSON Error 【发布时间】:2015-01-27 19:07:19 【问题描述】:当我尝试将 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
但我已经尝试过了,但没有帮助 - 我会的再试一次 - 但如果您发现此代码有问题,请告诉我:
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;
【问题讨论】:
您从服务器发送的 JSON 是一个数组,但您将它分配给NSDictionary
。我不能 100% 确定这是您的问题,但您可能应该更加小心。
@IanMacDonald 我更新了我的问题...我会尝试将它存储在数组而不是字典中...但我认为字典也应该可以工作。
@IanMacdonald - 我试过NSError *jsonError = nil; NSArray *jsonArray = (NSArray *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
,我得到了同样的错误。
您的输出是 HTML。您似乎没有收到您认为收到的回复。您可能不应该在单独的行上输出每个字符;将它们放在一个字符串中可以更容易阅读...<br /><font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
@IanMacDonald 你能详细说明一下吗?我注意到一件事...我在 Firefox 中使用 httprequester 进行测试...它说我没有收到响应...但是如果我从终端使用 curl - 我会收到响应...为什么?
【参考方案1】:
这听起来可能很愚蠢 - 但您确定您要的是正确的网址吗?请参阅我遇到相同问题的其他帖子。 https://***.com/a/28393273/557362
首先从 URL 响应开始,看看实际发生了什么。
你可以通过 NSLog [NSString stringWithUTF8String:[data bytes]]
来获取不错的数据版本,但不要忘记查看 HTTP 响应。
登录[response description]
。
【讨论】:
我最终通过切换到 afnetworking 并将 json 对象解释为数组而不是 nsdictionary 来解决问题 - 我现在意识到也许我需要做的就是将数据解释为数组而不是 nsdictionary在仍然使用 nsurlsession 的同时解决问题...以上是关于NSDebugDescription=字符 0 周围的值无效。- JSON 错误的主要内容,如果未能解决你的问题,请参考以下文章
错误域 = NSCocoaErrorDomain 代码 = 3840 “字符 1 周围的值无效。” UserInfo=NSDebugDescription=字符 1 周围的值无效。swift 4
错误域=NSCocoaErrorDomain 代码=3840 “无值。” UserInfo=NSDebugDescription=无值。
错误域 = NSCocoaErrorDomain 代码 = 3840 “无值。” UserInfo=NSDebugDescription=无值。
iOS JSON 错误:NSDebugDescription=Garbage at end
Json 错误:错误域 = NSCocoaErrorDomain 代码 = 3840“无值”。 UserInfo=NSDebugDescription=无值。
如何修复“错误域 = NSCocoaErrorDomain 代码 = 3840”无价值。“UserInfo = NSDebugDescription = 无价值。”