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条)

报告相同问题?

悬赏问题

  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题