我在PHP中有一个键值数组。例如:
<?php
$url = "https://mywebsite.com";
$resultArray = array(
"error" => "null",
"url" => $url,
);
echo json_encode($resultArray);
?>
**Output:** {"error":"null","url":"https:\/\/mywebsite.com"}
如上所述,当我回显json编码输出时。浏览器本身用每个尾部斜杠“/”添加反斜杠“\”。意味着如果我在json键值对中传递“https://mywebsite.com”,我得到的结果就是这样的“https://mywebsite.com”。但我不想要反斜杠。我想回显一个普通的URL ..我使用了urlencode方法,但是将斜杠转换为编码格式..我怎么能解决这个问题..我在网上搜索了很多主题,但我没有得到我正在寻找的确切输出在..
答案 0 :(得分:1)
Use JSON_UNESCAPED_SLASHES It will work
echo json_encode($resultArray, JSON_UNESCAPED_SLASHES);
答案 1 :(得分:0)
在我看来Magic Quotes正在开启。使用stripslashes
或关闭Magic Quotes。
答案 2 :(得分:0)
您可以测试内置的php函数stripslashes
或rawurldecode
。
<?php
$url = "https://mywebsite.com";
$resultArray = array(
"error" => "null",
"url" => stripslashes($url), //or try this rawurldecode($url)
);
echo json_encode($resultArray);
?>
答案 3 :(得分:0)
你是对的,你的网址没有错。你得到了正确的输出。当您再次解码此json数据时,您将获得原始输出。
$json_data = json_encode($resultArray);
$obj = json_decode($json_data, true);
echo $output_rul = $obj['url'];