Php 类stdClass的对象无法转换为字符串错误
我有以下字符串:Php 类stdClass的对象无法转换为字符串错误,php,json,string,object,Php,Json,String,Object,我有以下字符串: {"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp"
{"Coords":[{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.277720488429026","Longitude":"-9.012038778269686","Timestamp":"Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27770755361785","Longitude":"-9.011979642121824","Timestamp":"Fri Jul 05 2013 12:02:09 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"},{"Accuracy":"65","Latitude":"53.27769091555766","Longitude":"-9.012051410095722","Timestamp":"Fri Jul 05 2013 12:02:17 GMT+0100 (IST)"}],"Scan":"Whatever"}
我想用php来解码。该字符串是通过sql查询获得的。见下面的代码:
$TrackDetails_Query= "SELECT * FROM Tracks WHERE TrackID='".$TrackNum."' ORDER BY TrackID DESC";
$TrackDetails_Result= mysql_query($TrackDetails_Query) or die (mysql_error());
if (mysql_num_rows($TrackDetails_Result)==0){
echo 'There are no tracks for the number entered';
}
else{
$traces=$row['Traces'];
$decoded_traces=json_decode($traces);
echo $decoded_traces;
}
}
但我得到了一个错误:
Catchable fatal error: Object of class stdClass could not be converted to string
出现错误是因为您试图将stdClass对象转换为字符串,而该对象不支持该字符串 而不是
echo$decoded\u traces
尝试var\u dump($decoded\u traces)
——这将提供您解码的对象的诊断视图(我想这是您想要的)。你会发现它看起来像这样
class stdClass#1 (2) {
public $Coords =>
array(5) {
[0] =>
class stdClass#2 (4) {
public $Accuracy =>
string(2) "65"
public $Latitude =>
string(18) "53.277720488429026"
public $Longitude =>
string(18) "-9.012038778269686"
public $Timestamp =>
string(39) "Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"
}
[1] => (more of the same, edited for brevity)
[2] =>
[3] =>
[4] =>
}
public $Scan =>
string(8) "Whatever"
}
默认情况下,将创建如上所示的stdClass对象。如果您喜欢关联数组,请将true
作为第二个参数传递给json\u decode
,例如$decoded\u traces=json\u decode($traces,true)代码>
另一方面,虽然stdClass不能转换成字符串,但是您自己的类可以转换成字符串——实现magic方法的类可以转换成字符串 $decoded_traces
是一个对象。您不能简单地回显对象,因为这毫无意义
如果要调试对象,请使用var\u dump($decoded\u traces)
使用json\u encode($traces)
这将数组转换为字符串json_decode()
用于将字符串转换为数组或对象数组
$decoded_traces=json_decode($traces,true)代码>
而不是这个
$decoded_traces=json_decode($traces)代码>
它将把stdClass转换为array。请注意,$decoded\u traces
是一个数组,您可以根据需要使用。这是一个老问题,仍然需要回答,以便向像我这样刚刚遇到它的人澄清
尝试json_编码($traces)
就我而言
return var_dump(json_encode($result[0]));
结果是:
string(34) "{"id":1,"content":"test database"}"
而不是
$decoded_traces=json_decode($traces);
echo $decoded_traces;
试一试
您希望看到$decoded\u traces
对象的哪一部分被回响?您问题的标题具有误导性-我建议将其更改为“类stdClass的对象无法转换为字符串”。标题与问题正文有什么关系?您希望看到echo$decoded\u traces
要做什么,因为这是解码JSON的结果?@deceze我忘记更改标题了,非常抱歉!当我无意中使用“编码”而不是“解码”时,这种情况就发生了。我意识到了这一点,并在内容中更改了问题,但没有更改标题。
$decoded_traces=json_decode($traces, true);
print_r $decoded_traces;