将HttpFoundation \ Response转换为数组

时间:2017-06-14 21:32:14

标签: arrays symfony doctrine

我的symfony请求是返回带有实体对象和setstate方法的数组。

这是数组的转储。

array (
  0 => 
  HealthyLiving\ApiBundle\Entity\User::__set_state(array(
     'id' => 1,
     'username' => 'admin',
     'password' => '123',
     'email' => 'batoosay@gmail.com',
     'isActive' => false,
  )),
)

以下是代码:

public function loginAction()
{
  $restresult = $this->getDoctrine()->getRepository('ApiBundle:User')->findAll();
    if ($restresult === null) {
      return new View("there are no users exist", Response::HTTP_NOT_FOUND);
 }
    echo '<pre>' . var_export($restresult, true) . '</pre>';die;

   return new JsonResponse(
        $restresult
    );
}

由于奇怪的数组,JsonResponse是空的。如何将此对象数组转换为json?

1 个答案:

答案 0 :(得分:1)

尝试使用JMS序列化:

$serializer = $this->get('jms_serializer');

return new Response(
    $serializer->serialize($restresult, 'json')
);