【问题标题】:How can i print json file that is produced from ParseHub on screen?如何在屏幕上打印从 ParseHub 生成的 json 文件?
【发布时间】:2017-10-26 21:13:55
【问题描述】:

我使用 ParseHub 从基于 Ajax 的网站获取数据。数据以 json 文件格式返回。几个小时以来,我一直在努力打印返回的 json 文件,但未能成功。这是我的php代码:

<?php
header('Content-Type: application/json;charset=utf-8');

$params = http_build_query(array(
"api_key" => "xxxxx",
"format" => "json"
));

$result = file_get_contents(
'https://www.parsehub.com/api/v2/projects/{MY_RUN_TOKEN}/last_ready_run/data?'.$params,
false,
stream_context_create(array(
    'http' => array(
        'method' => 'GET'
    )
))
);

echo $result;


?>

顺便说一句,json_decode() 函数不起作用。谁能帮帮我?

更新:我上传了代码和代码的输出。您可以检查以提供一些想法。

代码 -> 这个link 输出 -> 这个link

【问题讨论】:

  • 你可能已经检查过了,但是 URL 是正确的并且 $result 是一个字符串? (不是布尔值)
  • 能给出完整代码吗?带json_decode()函数
  • 你也可以给转储的 $result 变量吗?
  • @billrichards 我添加了 gzdecode($result) 函数,现在我可以将其打印为字符串。但我需要把它当作一个json。

标签: php json parsehub


【解决方案1】:

来自ParseHub API documentation

此响应的 Content-Encoding 始终为 gzip。

尝试为您的 $result 变量使用gzdecode 函数。然后用 json_decode() 解码。

请注意,它需要 PHP 5 >= 5.4.0 或 PHP 7 版本。 您也可以尝试使用Parsehub REST api wrapper class

【讨论】:

  • 是的 gzdecode 有效。谢谢你的帮助。我在得到 $result 后添加了这一行: $data=gzdecode($result);回显$数据;但是 gzdecode 返回字符串。所以 $data 不是 json。现在,问题是将字符串 $data 转换为 json。 :)
  • 当我尝试使用:echo json_decode($data);它抛出该错误:“可捕获的致命错误:类 stdClass 的对象无法在第 20 行的 C:\xampp\htdocs\a.php 中转换为字符串”
  • 我上传了最终代码和代码输出。你能检查一下并给出一些想法吗?代码 -> 这个link 输出 -> 这个link
  • 实际上,当我在 postman 应用程序上运行 php 代码并以 json 模式查看结果时,我可以看到 json 格式的结果。我不确定,但问题看起来已经解决了。这是我看到的:link
  • 是的,这是 JSON 数据。使用json_decode($data, true);对其进行解码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
相关资源
最近更新 更多