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);
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:
(source: json.org)
You'll have to decode it before usage.
json_decode()
Escaped solidus is legal. But if you want a result without escaping, use JSON_UNESCAPED_SLASHES
in json_encode option. However, this was added after PHP 5.4. So, str_replace('\\/', '/', $pl2);
would be helpful.
That's what json_encode
is supposed to do. Once you json_decode
or JSON.parse
it, it's fine.
var f = {"a":"\/users\/crazy_bash\/online\/test\/"}
console.log(f.a); // "/users/crazy_bash/online/test/"
var h = JSON.parse('{"a":"\/users\/crazy_bash\/online\/test\/"}');
console.log(h.a); // "/users/crazy_bash/online/test/"
I had the same problem, basically you need to decode your data and then do the encode, so it works correctly without bars, check the code.
$getData = json_decode($getTable);
$json = json_encode($getData);
header('Content-Type: application/json');
print $json;