【发布时间】:2016-01-16 23:54:18
【问题描述】:
所以我有我的 php 脚本通过 http GET 请求从http://www.reddit.com/search.json 接收一个 json 对象,并且通过之前的测试,确实成功地获得了一个 json 对象。但是,当我在这个对象上使用 json_decode 时,我得到一个错误:
可捕获的致命错误:第 5 行 /home/hudson/ug/xbvg52/public_html/stupid.php 中的 stdClass 类的对象无法转换为字符串
这是我的(非常简单的)代码:
$query = $_GET["radio"];
$url = "https://www.reddit.com/search.json?q=".$query;
$response = file_get_contents($url);
echo json_decode($response);
如何将此 JSON 对象转换为字符串?
【问题讨论】:
-
$query 有什么价值?
-
你得到的响应是一个字符串,json_decode将它转换成一个对象(数组)
-
@Quentin $query 在上一页的 html 表单中输入了任何内容
-
@HJGBAUM — 显然,但除非你给我们举个例子,否则很难重现这个问题。
-
print_r (json_decode($response));json_decode 的结果是一个对象,所以echo不会工作