【问题标题】:Function Json_encode adds a \ behind every /函数 Json_encode 在每个 / 后面添加一个 \
【发布时间】:2016-08-26 20:42:40
【问题描述】:

我正在尝试使用带有 JSON 编码和 JSON 解码的 PHP 来编辑一些 JSON 文件。

JSON 文件中有一些明显使用“/”的 URL。

当我使用 JSON_decode 并打印文本时,我得到如下信息:

url = http://www.example.com/something/hello_1.0;

然后我运行这个脚本:

$new['versions']        = array();
$new['versions'][$version]  = current( $decode['versions'] );
foreach( $decode['versions'] as $sVersion => $aVersion ) {
    $new['versions'][$sVersion] = $aVersion;
}
$decode['versions']= $new['versions'];
$encode = json_encode($decode,JSON_PRETTY_PRINT);

添加一个新版本,但是结果是:

url = http:\/\/www.example.com\/something\/hello_1.1;
url = http:\/\/www.example.com\/something\/hello_1.0;

当我打印 json_decode 数组时,它仍然有正确的“/”。

有什么想法吗?

【问题讨论】:

标签: php json


【解决方案1】:

您可以使用json_encode 选项来实现您想要的。 特别是需要JSON_UNESCAPED_SLASHES 选项。 见here

例子:

$url = 'http://www.example.com/something/hello_1.0;'
echo json_encode($url, JSON_UNESCAPED_SLASHES);
//Prints: "http://www.example.com/something/hello_1.0;"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 2017-12-27
    • 2011-09-04
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    相关资源
    最近更新 更多