Symfony - Deserialize json to an array of entities

后端 未结 5 1722
无人及你
无人及你 2020-12-28 08:38

I have a json object that I received by making a get API call. I make this call to receive a list of objects. It\'s a list of post... So I have an array of Post Objects.

5条回答
  •  遥遥无期
    2020-12-28 09:10

    A less than ideal solution that I found was to first decode and then encode the json data again at the node that represents the data array. For example in your case:

    $json = json_decode($json);
    $json = json_encode($json->data);
    $serializer->deserialize($json, 'array', 'json');
    

    There must be a better solution than this but this seems more elegant than the above solution of de-serialising json.

提交回复
热议问题