So i have the following and can't seem to covert it to a normal JSON format, I am using JMS Serializer for APIs in the FOSRestBundle.
My line that gets sent to JMS Serializer is
return array(
'fields' => $entities,
);
$entities includes all the info below, what you see below is a
print_r($entities);
Output:
Array
(
[0] => Test\ClientBundle\Entity\Fieldset Object
(
[fieldid:Test\ClientBundle\Entity\Fieldset:private] => 43
[title:Test\ClientBundle\Entity\Fieldset:private] => Genre
[formid:Test\ClientBundle\Entity\Fieldset:private] => 1
[choicestext:Test\ClientBundle\Entity\Fieldset:private] => Array
(
[0] => stdClass Object
(
[label] => Folk
)
)
[defaultval:Test\ClientBundle\Entity\Fieldset:private] => 0
)
)
When i use the built in JMS Serializer i get this:
{"fields":[{"fieldid":43,"title":"Genre","choicestext":"Array","defaultval":"0"}]
The problem is that choicestext has no nested JSON object but just a string "Array", how can I make the code insert it so it can be read as nesteed JSON object, is there a trick with JMS Serializer?
This is the actual array that gets inserted into the object property choicestext:
Array
(
[0] => stdClass Object
(
[label] => Folk
)
)
When printed as JSON using json_encode it comes out as:
[{"label":"Folk"}]
So that is correct, just don't see why when inserted in that entities it doesn't work?
UPDATE:
print_r(json_encode($entities));
Result:
[{},{},{},{},{},{}]
json_encode()this data structure?json_encodecannot access the properties of the entity, because they are private. Therefore you get an array with six empty JSON objects.