我有一个需要以下输出的脚本:
[{
"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
功能?!
如何更改输出格式?
答案 0 :(得分:2)
它只是错过了它的外部容器。
试试这个:
return new JsonResponse( array( array(
"id" => 288,
"title" => "Titanic",
"year" => "1997"
)) );
这应输出为:
[{"id":288,"title":"Titanic","year":"1997"}]
答案 1 :(得分:1)
您必须将items数组包含在父数组中:
return new JsonResponse(array(
array(
"id" => 288,
"title" => "Titanic",
"year" => "1997",
....
),
array(
"id" => 288,
"title" => "Titanic",
"year" => "1997",
....
)
));
答案 2 :(得分:1)
您必须将数据放在另一个数组中以创建项目数组。只需将现有数组包装在另一个数组中:
return new JsonResponse(array(
array(
"id" => 288,
"title" => "Titanic",
"year" => "1997",
....
)
));