11

我有以下字符串:

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

6 回答 6

28

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

而不是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"
}

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

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

于 2013-07-05T13:15:04.750 回答
7

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

于 2013-07-05T13:17:42.453 回答
4

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

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

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

于 2013-07-05T13:25:25.493 回答
4

这是一个古老的问题,仍然回答让像我这样遇到它的人清楚。

尝试 json_encode($traces);

就我而言

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

结果是:

string(34) "{"id":1,"content":"test database"}"
于 2017-08-02T10:58:59.230 回答
1

$decoded_traces是一个对象。你不能只是echo一个对象,因为那没有意义。

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

于 2013-07-05T13:15:19.280 回答
1

代替

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

尝试

$decoded_traces=json_decode($traces, true);
print_r $decoded_traces;
于 2019-03-20T17:13:14.390 回答