【问题标题】:PHP json_decode returns null with SymfonyPHP json_decode 使用 Symfony 返回 null
【发布时间】: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

标签: php json rest api symfony


【解决方案1】:

您正在尝试对数组进行编码,请将编码函数放在序列化列表之后:

/**
 * @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(utf8_encode($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($this->showActionListSerialize(), true);

    var_dump($articles);
    return $this->render('listeArticles.html.twig', array('articles' => $articles));

}

【讨论】:

  • 您能解释一下为什么在序列化列表后对数据进行编码吗? findAll 函数返回一个对象数组。事实上,当你调用 serialize 方法时,symfony Serializer 做了两件事。首先,它执行标准化以将数据转换为格式良好的数组。这是一个递归操作,因此规范器检查数据的类型并选择相应的规范器。由于它是一个 Article 对象的数组,因此每次找到 Article 时都会使用 ObjectNormalizer。然后在规范化步骤完成后,Symfony 序列化程序执行 json_encode。
  • 我说的是“utf8_encode”函数,这个函数只接受“string”类型的参数,在你的代码中,你返回一个“Reponse”类型的对象,所以函数“utf8_encode”将是应用于对象“响应”而不是序列化的 json。
  • 哦,好吧,但实际上这不是 $articles 变量为空的原因
【解决方案2】:

首先欢迎使用 StackOverflow,

为了实现您的目标,您的第二个控制器应如下所示:

/**
 * @Route("/api_articles_list/articles_list", name="articles_list")
 * @Method({"GET"})
 */
public function showActionList()
{
    $articles = json_decode(utf8_encode($this->showActionListSerialize()->getContent()), true);

    var_dump($articles);

    return $this->render('listeArticles.html.twig', array('articles' => $articles));

}

首先使用 utf8_encode 不是强制性的。然后你的方法 showActionListSerialize 返回一个 HttpFoundation Response 对象。

当您使用这样的序列化数据实例化响应时:

$response = new Response($data);

其实和做的一样:

$response->setContent($data);

所以要访问 JSON 数据,您需要 getContent 方法。

实际输出

var_dump(utf8_encode($this->showActionListSerialize());

是:

HTTP/1.0 200 OK 缓存控制:无缓存,私有内容类型: 应用程序/json 日期:2018 年 11 月 26 日星期一 13:25:22 GMT [{“内容”:“洛伦 ipsum","vendeur":"test_vendeur","id":1,"title":"title_test"}]"

和输出

var_dump(utf8_encode($this->showActionListSerialize()->getContent());

[{"content":"Lorem ipsum","vendeur":"test_vendeur","id":1,"title":"title_test"}]

此解决方案应该适合您。你的树枝模板应该是这样的:

{% block body %}
    {% for article in articles %}
        <br>
        id: {{ article.id }} <br>
        title: {{ article.title }}<br>
        content: {{ article.content }}<br>
        vendeur: {{ article.vendeur }}
    {% endfor %}
{% endblock %}

【讨论】:

  • 不客气。无论如何,如果确实解决了问题,请不要忘记将此答案标记为解决您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-13
  • 2018-05-27
  • 1970-01-01
  • 2023-03-23
相关资源
最近更新 更多