duanjiaonie6097 2012-01-12 15:58
浏览 638
已采纳

PHP:json_encode将斜杠改为\ /

I have a variable which contains a path in json_encode /users/crazy_bash/online/test/

but json_encode converts the path to this:

 \/users\/crazy_bash\/online\/test\/

Why? How can i display a normal path?

the code

$pl2 = json_encode(array(
    'comment' => $nmp3,
    'file' => $pmp3
));
echo($pl2);
  • 写回答

5条回答 默认 最新

  • dragon4808 2012-01-12 16:02
    关注

    It's perfectly legal JSON, see http://json.org/. \/ is converted to / when unserializing the string. Why worry about it if the output is unserialized by a proper JSON parser?

    If you insist on having \/ in your output, you can use str_replace():

    // $data contains: {"url":"http:\/\/example.com\/"}
    $data = str_replace("\\/", "/", $data);
    echo $data; // {"url":"http://example.com/"}
    

    Note that it's still valid JSON by the definition of a string:

    JSON string
    (source: json.org)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出