编程语言
首页 > 编程语言> > php-如何在Symfony中格式化JSON输出

php-如何在Symfony中格式化JSON输出

作者:互联网

我有一个脚本,期望以下输出:

[{
    "id": "288",
    "title": "Titanic",
    "year": "1997",
    "rating": "7.7",
    "genre": "drama, romance",
    "od": "0"
}, {
    "id": "131",
    "title": "The Bourne Identity",
    "year": "2002",
    "rating": "7.9",
    "genre": "action, mystery, thriller",
    "od": "1"
}]

看起来格式不正确的json,当我这样做时:

return new JsonResponse(array(
        "id" => 288,
        "title" => "Titanic",
        "year" => "1997",
        ....
    ));

我得到这个:

{

"id": ​288,
"title": "Titanic",
"year": "1997"
....
}

我使用的插件是this,它甚至具有$.getJson Function?!?

如何更改输出格式?

解决方法:

它只是缺少外部容器.

尝试这个:

return new JsonResponse( array( array(
  "id"    => 288,
  "title" => "Titanic",
  "year"  => "1997"
)) );

这应该输出为:

[{"id":288,"title":"Titanic","year":"1997"}]

标签:symfony,response,jsonresponse,json,php
来源: https://codeday.me/bug/20191027/1946109.html