I am having trouble converting a basic valid JSON string object into a PHP object using json_decode (it shows as null). I have tried several things but so far still not working. I'm sure I am missing something simple but I could use help getting this going. This seems to be an issue about the JSON string that is coming directly from the database (text field) - possibly something related to escaping characters or something?
basic json string in database (text field) :
{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}
PHP
$jsonobj = $dbfield;
echo $jsonobj
on page display is:
string(187) "{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}"
var_dump($jsonobj);
on page display is:
string(187) "{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}"
php
var_dump(json_decode($jsonobj));
on page display is:
NULL - why?
php
var_dump(html_entity_decode($jsonobj));
on page display is:
string(87) "{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}"
this works when i create hard coded var...
$js = '{"address":"111 main street","id":"1","name":"test","phone":"888 888 8888","zip":"123"}';
$obj = json_decode($js);
echo $obj->address;
display works
111 main street
Can anyone shed some light on converting what looks like valid json from the database to php object?
187vs the actual character count, which is about 88. You have some invisible characters breaking your json string. This may be from when you're inserting it into the database