'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
Solution 1:[1]
You get the error because you are trying to turn a stdClass object into a string, something it doesn't support.
Instead of echo $decoded_traces
try 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
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_encode will create stdClass objects like the above. If you would prefer an associative array, pass true
as the second parameter to json_decode
, e.g. $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 __toString magic method could be converted to a string!
Solution 2:[2]
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
Solution 3:[3]
Try using this
$decoded_traces=json_decode($traces, true);
instead of this
$decoded_traces=json_decode($traces);
It will convert your stdClass to array. Note that $decoded_traces
is array then you can use as you require.
Solution 4:[4]
It was an old question, still answer to make it clear for someone who just encounters it like me.
try json_encode($traces);
in my case
return var_dump(json_encode($result[0]));
the result is:
string(34) "{"id":1,"content":"test database"}"
Solution 5:[5]
Instead of
$decoded_traces=json_decode($traces);
echo $decoded_traces;
try
$decoded_traces=json_decode($traces, true);
print_r $decoded_traces;
Solution 6:[6]
$decoded_traces
is an object. You cannot simply echo
an object, because that makes no sense.
If you want to debug the object, use var_dump($decoded_traces)
.
Solution 7:[7]
I've noticed that it can happen in wordpress too if you are using the $wpdb->get_row()
method and you miss adding the last parameter
You just need to add another parameter at the end of the function, so you'll get
$res = $wpdb->get_row("SELECT * FROM wp_options WHERE option_id = 1" ,ARRAY_A)
which will output an array instead of an stdClass then you just have to use it like this
$res['option_value']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Nabeel Arshad |
Solution 3 | dakshbhatt21 |
Solution 4 | serena feng |
Solution 5 | emmanuel agarry |
Solution 6 | deceze |
Solution 7 | Gui |