Bujanca Mihai Bujanca Mihai - 4 months ago 43
JSON Question

Object of class stdClass could not be converted to string- can't find a solution

I have the following code:

if ($difference)
{
$.post("recover_functions/upload_photos.php",
{upload:$difference},
function(response)
{
alert($difference);
if (response.status)
alert("Successfully uploaded photos");
else
alert(response);
});
}


The alert will display:

{"difference":"[{\"aid\":\"100000543443572_1073741825\",\"backdated_time\":null,\"caption\":\"\",\"link\":\"http:\\\/\\\/www.facebook.com\\\/photo.php?fbid=614604095234366&set=a.614604001901042.1073741825.100000543443572&type=1\",\"pid\":\"100000543443572_2384218\",\"place_id\":null}]"}


And I get the `POST request like this:

$string_diff = $_POST['upload'];
$array_diff = json_decode($string_diff);

echo $array_diff;


Not the response is
Object of class stdClass could not be converted to string
.
Please help, I have a deadline (1 hour) and I can't get this working.

Answer

Like an array, you can't echo a stdClass object. You could var_dump($array_diff) and echo specific properties like so:

echo $array_diff->my_property;