Harsha M V Harsha M V - 1 year ago 88
JSON Question

json_decode to array

I am trying to decode a JSON string into an array but i get the following error.

Fatal error: Cannot use object of type
stdClass as array in
C:\wamp\www\temp\asklaila.php on line

Here is the code:

$json_string = 'http://www.domain.com/jsondata.json';

$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata);

Answer Source

As per the documentation, you need to specify if you want an associative array instead of an object from json_decode, this would be the code:

json_decode($jsondata, true);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download