Strange behaviour: json_encode() producing invalid json dependent on input
I have no idea about whether this will help or not, but does running the following code produce any different results?
$obj = new stdClass();$obj->Id = '35';$obj->EventsCategories_Id = '8';$obj->Name = 'Blubber';$obj->OrderValue = '2';// convert the stdClass object to an array$obj = (array) $obj;var_dump($obj);json_encode($obj);
I think the pertinent question to ask is whether this is:
- an internal PHP issue (possible, but unlikely)
- a problem arising from some environmental issue (a conflicting library?)
- a missing configuration option
While I highly doubt that you've done something wrong (I mean, this is a simple use-case and there's nothing wrong with your code at all), perhaps you could try a couple of other things...
For example, try appending a 2nd argument to the json_encode
function:
echo json_encode($obj, JSON_FORCE_OBJECT);
Could you post the results of these two short experiments? This problem is quite strange, indeed. I'm running PHP 5.3.1 and it generates syntactically correct JSON based on your example - no problems to report.