php 类 stdClass 的对象无法转换为字符串错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17489771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:59:43  来源:igfitidea点击:

Object of class stdClass could not be converted to string error

phpjsonstringobject

提问by user2363025

I have the following string:

我有以下字符串:

{"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"}

Which I want to decode in php. The string is obtained via a sql query. See code below:

我想在 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;
            }
    }

But I am getting the error:

但我收到错误:

Catchable fatal error: Object of class stdClass could not be converted to string

回答by Paul Dixon

You get the error because you are trying to turn a stdClass object into a string, something it doesn't support.

您收到错误是因为您试图将 stdClass 对象转换为字符串,这是它不支持的。

Instead of echo $decoded_tracestry var_dump($decoded_traces)- that will give a diagnostic view of the object you've decoded (which I presume is what you wanted). You should find it looks like this

而不是echo $decoded_traces尝试var_dump($decoded_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"
}

By default, json_encodewill create stdClass objects like the above. If you would prefer an associative array, pass trueas the second parameter to json_decode, e.g. $decoded_traces=json_decode($traces, true);

默认情况下,json_encode会像上面一样创建 stdClass 对象。如果您更喜欢关联数组,true请将其作为第二个参数传递给json_decode,例如$decoded_traces=json_decode($traces, true);

As a further aside, although stdClass can't be turned into a string, your own classes can - a class which implements the __toStringmagic method could be converted to a string!

另外,虽然 stdClass 不能转换为字符串,但您自己的类可以 - 实现__toString魔术方法的类可以转换为字符串!

回答by Nabeel Arshad

use json_encode($traces)this will convert the array into the string. json_decode()is used to convert a string into array or object array

使用json_encode($traces)这会将数组转换为字符串。json_decode()用于将字符串转换为数组或对象数组

回答by dakshbhatt21

Try using this $decoded_traces=json_decode($traces, true);

尝试使用这个 $decoded_traces=json_decode($traces, true);

instead of this $decoded_traces=json_decode($traces);

而不是这个 $decoded_traces=json_decode($traces);

It will convert your stdClass to array. Note that $decoded_tracesis array then you can use as you require.

它会将您的 stdClass 转换为数组。请注意,这$decoded_traces是数组,然后您可以根据需要使用。

回答by serena feng

It was an old question, still answer to make it clear for someone who just encounters it like me.

老问题了,还是给我这种刚遇到的人说清楚。

try json_encode($traces);

试试 json_encode($traces);

in my case

就我而言

return var_dump(json_encode($result[0]));

the result is:

结果是:

string(34) "{"id":1,"content":"test database"}"

回答by emmanuel agarry

Instead of

代替

 $decoded_traces=json_decode($traces);
 echo $decoded_traces;

try

尝试

$decoded_traces=json_decode($traces, true);
print_r $decoded_traces;

回答by deceze

$decoded_tracesis an object. You cannot simply echoan object, because that makes no sense.

$decoded_traces是一个对象。你不能简单地echo成为一个对象,因为那毫无意义。

If you want to debug the object, use var_dump($decoded_traces).

如果要调试对象,请使用var_dump($decoded_traces).