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

1

I made a request and in the result brought $ result. But I wanted to access inside the string as an object and this error appears. I saw similar errors in other questions, but it was not clear to me.

echo $result;
echo "<br>";
echo var_dump($result);
echo "<br>";
echo json_decode($result);
{"response":{"dsLOGIN":{"dsLOGIN":{}}}}
string(39) "{"response":{"dsLOGIN":{"dsLOGIN":{}}}}" 

Error:

  

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

    
asked by anonymous 13.02.2017 / 12:33

1 answer

1

OBJECT OF CLASS STDCLASS

Using json_decode() results in an Object, or an Array of Objects ( Font ), and using ECHO in an Object causes the attempt to convert it to String, causing the Error:

  

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

To write an Object directly you must use print_r() thus displaying even its properties ( Font ).

In your case the following should work properly:

echo $result;
echo "<br>";
echo var_dump($result);
echo "<br>";
$jsonResult = json_decode($result);
print_r($jsonResult); // $jsonResult já é um Objeto
print_r($jsonResult->response) // $jsonResult->esponse também contém um Objeto
    
13.02.2017 / 13:12