0

使用symfony框架的是后, 如何将doctrine从数据库查询出来的对象以json对象的形式返回到页面?

1个回答

0
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
   
    public static function encode($object, $format = 'json')
    {
        $encoders    = [new XmlEncoder(), new JsonEncode()];
        $normalizers = [new GetSetMethodNormalizer()];
        $serializer  = new Serializer($normalizers, $encoders);
        $encodes     = $serializer->serialize($object, $format);
        return $encodes;
    }
    
    
    
    
 相关文档直接查看 http://symfony.com/doc/current/components/serializer.html   

撰写答案

SegmentFault

一起探索更多未知

下载 App