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

时间:2022-03-06 20:29:01

I am trying to edit some JSON files by using PHP with JSON enconde and JSON decode.

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

The JSON files has some URLs that obviously use '/'.

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

When I use JSON_decode and print the text, I get something like this:

当我使用JSON_decode并打印文本时,我会得到这样的结果:

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

Then I run this script:

然后我运行这个脚本:

$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);

To add a new version, however the result is:

要添加新版本,结果是:

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

When I print the json_decode array, it still has the '/' correctly.

当我打印json_decode数组时,它仍然正确地具有'/'。

Any ideas?

有任何想法吗?

1 个解决方案

#1


2  

You can use the json_encode options to achieve what you want. Especially the JSON_UNESCAPED_SLASHES option is needed. See here

您可以使用json_encode选项来实现您想要的效果。特别需要JSON_UNESCAPED_SLASHES选项。看这里

Example:

例:

$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;"

#1


2  

You can use the json_encode options to achieve what you want. Especially the JSON_UNESCAPED_SLASHES option is needed. See here

您可以使用json_encode选项来实现您想要的效果。特别需要JSON_UNESCAPED_SLASHES选项。看这里

Example:

例:

$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;"