【发布时间】:2019-04-27 11:01:42
【问题描述】:
我正在使用 Symfony 开发 REST API,但我的 json_decode 函数在我的数组中返回 null。我已经看到他可能是编码问题,但我不确定。 这是我的 json :
{
"id": 1,
"title": "Titre",
"content": "Contenu",
"vendeur": null
}
这是我序列化和解码 json 的函数:
/**
* @Route("/api_articles_list", name="api_articles_list")
* @Method({"GET"})
*/
public function showActionListSerialize()
{
$articles = $this->getDoctrine()->getRepository('AppBundle:Article')->findAll();
// return new JsonResponse(array('articles' => $articles));
$data = $this->get('serializer')->serialize($articles, 'json');
$response = new Response($data);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* @Route("/api_articles_list/articles_list", name="articles_list")
* @Method({"GET"})
*/
public function showActionList()
{
$articles = json_decode(utf8_encode($this->showActionListSerialize()), true);
var_dump($articles);
return $this->render('listeArticles.html.twig', array('articles' => $articles));
}
【问题讨论】:
-
$articles 在 showActionListSerialize 中返回什么?我想你需要这样写 $this->getDoctrine()->getManager()->getRepository('AppBundle:Article')->findAll()
-
请提供minimal reproducible example。此外,您不解码数组,您可能想在您的问题中澄清这一点。也就是说,作为新用户,请使用tour 并阅读How to Ask。