I pass the JSON encoded string to json_decode() and expect its output to be an object type, but instead I get a string type. How can I return an object?
In documents, the following returns an object:
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json));
However, if I json_encode() string first, and then call json_decode() , the output will be a string, not an object:
$json = json_encode('{"a":1,"b":2,"c":3,"d":4,"e":5}'); var_dump(json_decode($json));
This is just a simplified example. In practice, what I'm doing is pushing a JSON encoded string in PHP through AJAX. However, it illustrates the problem of converting this encoded JSON string to an object that I can read in PHP, for example, " $json->a ".
How to return an object type?
thanks for answers! The actual context for this question, I used the JSON Response from the API. But when I do json_decode for this answer and try to access values ββlike - $ json = json_decode (json response from the API); echo $ json-> a this gives me an error: an object of class stdClass cannot be converted to a string
source share