Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object of class stdClass could not be converted to string error

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:

$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
like image 865
user2363025 Avatar asked Jul 05 '13 13:07

user2363025


2 Answers

Instead of

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

try

$decoded_traces=json_decode($traces, true);
print_r $decoded_traces;
like image 87
emmanuel agarry Avatar answered Sep 28 '22 06:09

emmanuel agarry


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

like image 44
Nabeel Arshad Avatar answered Sep 28 '22 06:09

Nabeel Arshad