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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证