I have a JavaScript object inside a string and I want to use php to turn this into a JSON. How would you go about this?
<?php $string = '{trees : {key: "value", id: 1}, animals : {key:"value", id: 2}}'; ?>
Important:
This string is not a JSON, but it is a valid JavaScript inside a string. The keys don't have quotes, only the values have quotes (if they are string). So json_decode($string)
will not work in this case.
Desired result:
{
"trees": {
"key": "value",
"id": "1"
},
"animals": {
"key": "value",
"id": "2"
}
}
I want to turn it into a JSON in php (serverside) so I can pass it back to my JavaScript, decode it and use it as an object.
Or can I just pass it as a string to JS and turn it into an object there? That would also work.
NOTE: I can not change how the string is saved in database. It is and it will remain a string.