json_decode 不解码服务器的响应?

发布于 2024-12-24 02:22:35 字数 994 浏览 5 评论 0原文

我正在尝试解码硒服务器的响应。服务器返回:

{"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 技术交流群。

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

发布评论

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

评论(1

情话难免假 2024-12-31 02:22:36

我猜测 $s->result 是 HTML 响应正文,它不会以 UTF-8 编码数据的形式返回(因此 json_encode 返回 NULL )。这是服务器端的问题,因为 JSON 应该采用 UTF-8 编码。理想情况下,服务器会使用 Content-Type 标头进行响应,告诉您响应正文的编码。

不过,您可以通过对响应调用 utf8_encode 来解决此问题:

$json = json_decode(utf8_encode($s->result));
echo '<pre>' . print_r($json, 1) . '</pre>';

只有当响应采用 ISO-8859-1 格式时,此方法才有效。作为附加检查,您可能需要使用 mb_detect_encoding。然后,您可以将结果传递到 iconv:

$json = json_decode(iconv($sourceEncoding, 'UTF-8', $s->result));
echo '<pre>' . print_r($json, 1) . '</pre>';

如果所有其他方法都失败,请查看 json_last_error

if ($json === null) {
    var_dump(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 (so json_encode returns NULL). 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:

$json = json_decode(utf8_encode($s->result));
echo '<pre>' . print_r($json, 1) . '</pre>';

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:

$json = json_decode(iconv($sourceEncoding, 'UTF-8', $s->result));
echo '<pre>' . print_r($json, 1) . '</pre>';

If all else fails, have a look at the output of json_last_error:

if ($json === null) {
    var_dump(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 with str_replace("\0", '', $s->result).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文