json_decode 不解码服务器的响应?
我正在尝试解码硒服务器的响应。服务器返回:
{"sessionId":null,"status":0,"value":{"os":{"arch":"amd64","name":
"Windows Server 2008 R2","version":"6.1"},"java":{"version":"1.7.0_02"},
"build":{"revision":"15105","time":"2011-12-08 09:56:25","version":"2.15.0"}},
"class":"org.openqa.selenium.remote.Response","hCode":1813953336}
并且我尝试使用以下内容对其进行解码:
$json = json_decode($s->result);
echo '<pre>'.print_r($json, 1).'</pre>';
在这个阶段, $s 对象是:
Scrape Object
(
[headers] => Array
(
)
[result] => {"sessionId":null,"status":0,"value":{"os":{"arch":"amd64","name":"Windows Server 2008 R2","version":"6.1"},"java":{"version":"1.7.0_02"},"build":{"revision":"15105","time":"2011-12-08 09:56:25","version":"2.15.0"}},"class":"org.openqa.selenium.remote.Response","hCode":287101789}
[http_code] => 200
[error] =>
)
但是,当我实际将结果粘贴到 json_decode() 中时,它做得很好吗?我哪里出错了?
I'm trying to decode a selenium server's response. The server returns:
{"sessionId":null,"status":0,"value":{"os":{"arch":"amd64","name":
"Windows Server 2008 R2","version":"6.1"},"java":{"version":"1.7.0_02"},
"build":{"revision":"15105","time":"2011-12-08 09:56:25","version":"2.15.0"}},
"class":"org.openqa.selenium.remote.Response","hCode":1813953336}
and i'm trying to decode it with the following:
$json = json_decode($s->result);
echo '<pre>'.print_r($json, 1).'</pre>';
At this stage the $s object is:
Scrape Object
(
[headers] => Array
(
)
[result] => {"sessionId":null,"status":0,"value":{"os":{"arch":"amd64","name":"Windows Server 2008 R2","version":"6.1"},"java":{"version":"1.7.0_02"},"build":{"revision":"15105","time":"2011-12-08 09:56:25","version":"2.15.0"}},"class":"org.openqa.selenium.remote.Response","hCode":287101789}
[http_code] => 200
[error] =>
)
However when I actually paste the results into json_decode() it does it just fine? Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测
$s->result
是 HTML 响应正文,它不会以 UTF-8 编码数据的形式返回(因此json_encode
返回NULL )。这是服务器端的问题,因为 JSON 应该采用 UTF-8 编码。理想情况下,服务器会使用 Content-Type 标头进行响应,告诉您响应正文的编码。
不过,您可以通过对响应调用
utf8_encode
来解决此问题:只有当响应采用 ISO-8859-1 格式时,此方法才有效。作为附加检查,您可能需要使用 mb_detect_encoding。然后,您可以将结果传递到 iconv:
如果所有其他方法都失败,请查看 json_last_error:
编辑:本例中的错误是
JSON_ERROR_CTRL_CHAR
;响应包含许多 NUL 字符,已使用str_replace("\0", '', $s->result)
删除这些字符。I would guess that
$s->result
is the HTML response body, and it's not coming back as UTF-8–encoded data (sojson_encode
returnsNULL
). This is an issue on the server side, as JSON should be UTF-8–encoded. Ideally, the server would respond with a Content-Type header telling you the encoding of the response body.However, you can work around the issue by calling
utf8_encode
on the response:This will only work if the response is in ISO-8859-1. As an additional check, you may want to detect the encoding of the response using mb_detect_encoding. You can then pass the result into
iconv
:If all else fails, have a look at the output of json_last_error:
EDIT: The error in this case was
JSON_ERROR_CTRL_CHAR
; the response contained a number of NUL characters, which were removed withstr_replace("\0", '', $s->result)
.