函数Json_encode在每个/后面添加\

时间:2016-05-02 06:45:25

标签: php json

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

JSON文件有一些显然使用'/'。

的网址

当我使用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数组时,它仍然正确地显示'/'。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用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;"