3

我有一个需要以下输出的脚本:

[{
    "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,它甚至还有一个$.getJsonFunction?!?

我将如何更改输出格式?

4

3 回答 3

2

它只是缺少它的外部容器。

尝试这个:

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

这应该输出为:

[{"id":288,"title":"Titanic","year":"1997"}]
于 2015-11-24T08:51:30.077 回答
1

您必须将数据放在另一个数组中才能创建项目数组。只需将现有数组包装在另一个数组中:

return new JsonResponse(array(
    array(
        "id" => 288,
        "title" => "Titanic",
        "year" => "1997",
        ....
    )
));
于 2015-11-24T08:52:00.337 回答
1

您必须将 items 数组包含到父数组中:

return new JsonResponse(array(
    array(
        "id" => 288,
        "title" => "Titanic",
        "year" => "1997",
        ....
    ),
    array(
        "id" => 288,
        "title" => "Titanic",
        "year" => "1997",
        ....
    )
));
于 2015-11-24T08:51:24.733 回答