doupao5296 2016-02-22 09:06
浏览 70
已采纳

Symfony从输出序列化实体转义反斜杠

Hi I am using Symfony2 for my application. I am using the serializer component.

    $encoder = new JsonEncoder();
    $normalizer = new GetSetMethodNormalizer();

    $callback = function ($dateTime) {
        return $dateTime instanceof \DateTime
            ? $dateTime->format(\DateTime::ISO8601)
            : '';
    };

    $normalizer->setCallbacks(array('matchAStartTime' => $callback, 'matchBStartTime'=> $callback, 'matchDate'=> $callback));
    $normalizer->setIgnoredAttributes(array('createdAt', 'updatedAt'));
    $serializer = new Serializer(array($normalizer), array($encoder));
    $json = $serializer->serialize($entity, 'json');

but in the output i am having response like this:

\"id\":1,\"matchAStatus\":\"Live\"

my question is how can I remove that slash in output? I know in raw php there is option for escape backslash but what can I use in Symfony?

  • 写回答

3条回答 默认 最新

  • douwen1915 2016-02-22 09:31
    关注

    You can use JSON_UNESCAPED_SLASHES constant (php >= 5.4.0).

    use Symfony\Component\Serializer\Encoder\JsonDecode;
    use Symfony\Component\Serializer\Encoder\JsonEncode;
    
    $encoder = new JsonEncoder(new JsonEncode(JSON_UNESCAPED_SLASHES), new JsonDecode(false))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?