json - 如何使用序列化程序(Symfony 4)创建正确格式的 json 字符串?
问题描述
我正在使用序列化程序创建一个 json 字符串:
$table = $this->getDoctrine()->getRepository(Article::class)->findAll();
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$json_string = $serializer->serialize($table, 'json');
$table 的结果:
array(2) { [0]=> object(App\Entity\Article)#5975 (3) { ["id":"App\Entity\Article":private]=> int(1) ["title":"App\Entity\Article":private]=> string(9) "Article 1" ["body":"App\Entity\Article":private]=> string(32) "This is the body for article one" } [1]=> object(App\Entity\Article)#5979 (3) { ["id":"App\Entity\Article":private]=> int(2) ["title":"App\Entity\Article":private]=> string(11) "Article Two" ["body":"App\Entity\Article":private]=> string(32) "This is the body for article two" } }
这是 $json_string 的结果:
string(145) "[{"id":1,"title":"Article 1","body":"This is the body for article one"},{"id":2,"title":"Article Two","body":"This is the body for article two"}]"
但这不是我需要的正确格式。我需要这样编码:
{
"recordsTotal": 10,
"recordsFiltered": 10,
"draw": 1,
"data": [
[
"Airi",
"Satou",
"Accountant",
"Tokyo",
"28th Nov 08",
"$162,700"
],
[
"Angelica",
"Ramos",
"Chief Executive Officer (CEO)",
"London",
"9th Oct 09",
"$1,200,000"
],
[
"Ashton",
"Cox",
"Junior Technical Author",
"San Francisco",
"12th Jan 09",
"$86,000"
],
[
"Bradley",
"Greer",
"Software Engineer",
"London",
"13th Oct 12",
"$132,000"
],
[
"Brenden",
"Wagner",
"Software Engineer",
"San Francisco",
"7th Jun 11",
"$206,850"
],
[
"Brielle",
"Williamson",
"Integration Specialist",
"New York",
"2nd Dec 12",
"$372,000"
],
[
"Bruno",
"Nash",
"Software Engineer",
"London",
"3rd May 11",
"$163,500"
],
[
"Caesar",
"Vance",
"Pre-Sales Support",
"New York",
"12th Dec 11",
"$106,450"
],
[
"Cara",
"Stevens",
"Sales Assistant",
"New York",
"6th Dec 11",
"$145,600"
],
[
"Cedric",
"Kelly",
"Senior Javascript Developer",
"Edinburgh",
"29th Mar 12",
"$433,060"
]
]
}
使用归一化器的方法:
$normalizer = new ObjectNormalizer();
$encoder = new JsonEncoder();
$serializer = new Serializer(array($normalizer), array($encoder));
$json_string = $serializer->serialize($table, 'json');
结果:
string(145) "[{"id":1,"title":"Article 1","body":"This is the body for article one"},{"id":2,"title":"Article Two","body":"This is the body for article two"}]"
解决方案
我知道这是从 2018 年开始的。但我需要给出这个解决方案,经过几个小时的研究,我发现了这个:
$response = new Response($serializer->serialize($data, JsonEncoder::FORMAT, [JsonEncode::OPTIONS => JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT]));
$response->headers->set('Content-Type', 'application/json');
return $response;
在 Symfony 4.2 中对我来说非常好用
推荐阅读
- c - 用 C 语言编写一个程序来读取和打印一个班级的 n 个学生的名字
- asp.net-core - 在 Blazor Web Assembly 项目中使用 SASS 的最佳方式是什么?
- c# - 如何在c#中获取执行命令窗口
- reactjs - 多步表单问题
- c# - TLS/SSL RABBIT MQ (Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..)
- tsql - 比较 2 个字符串并获得匹配 - T SQL
- sql - 在 2 个表之间结合 row_num 和 group by
- http - 后端不响应 HTTP 请求
- git - 有没有办法强制只有符合此分支策略的合并才会被 github 存储库接受?
- python - 如何在 Google Cloud 上部署 Google colab (Jyputer NoteBook) 以自动运行?