如何使用序列化器(Symfony 4)以正确的格式创建json字符串?

时间:2018-07-24 07:11:19

标签: json symfony doctrine encode serializer

我正在使用序列化程序创建一个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"}]" 

3 个答案:

答案 0 :(得分:1)

您可以通过JsonSerializable界面进行控制。

class Article implements JsonSerializable {
public function jsonSerialize() {
        $arr = [
            $this->firstName,
            $this->secondName,
            //...
            ];
        return $arr;
    }
}

答案 1 :(得分:1)

面对同样的问题,找到了解决方案:

    $array = json_decode($serializer->serialize($object, 'json'), true);
    $entryJSONFile = json_encode($array, JSON_PRETTY_PRINT);

这实际上可以解决问题,但是我对双重转换仍然不满意。

答案 2 :(得分:1)

我知道这是从2018年开始的。但是经过几个小时的研究,我发现了这个问题:

$response = new Response($serializer->serialize($data, 'json', ['json_encode_options' => JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT]));
$response->headers->set('Content-Type', 'application/json');
return $response;

在Symfony 4.2中对我来说工作正常