[英]How to format JSON output in Symfony
我有一个期望以下输出的脚本:
[{
"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"
....
}
我正在使用的插件就是这个 ,它甚至还有一个$.getJson
函数?!?
我该如何更改输出格式?
它只是错过了它的外部容器。
试试这个:
return new JsonResponse( array( array(
"id" => 288,
"title" => "Titanic",
"year" => "1997"
)) );
这应输出为:
[{"id":288,"title":"Titanic","year":"1997"}]
您必须将items数组包含在父数组中:
return new JsonResponse(array(
array(
"id" => 288,
"title" => "Titanic",
"year" => "1997",
....
),
array(
"id" => 288,
"title" => "Titanic",
"year" => "1997",
....
)
));
您必须将数据放在另一个数组中以创建项目数组。 只需将现有数组包装在另一个数组中:
return new JsonResponse(array(
array(
"id" => 288,
"title" => "Titanic",
"year" => "1997",
....
)
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.