Json_Encode() Escaping Forward Slashes

json_encode() escaping forward slashes

is there a way to disable it?

Yes, you only need to use the JSON_UNESCAPED_SLASHES flag.

!important read before: https://stackoverflow.com/a/10210367/367456 (know what you're dealing with - know your enemy)

json_encode($str, JSON_UNESCAPED_SLASHES);

If you don't have PHP 5.4 at hand, pick one of the many existing functions and modify them to your needs, e.g. http://snippets.dzone.com/posts/show/7487 (archived copy).

Example Demo

<?php
/*
* Escaping the reverse-solidus character ("/", slash) is optional in JSON.
*
* This can be controlled with the JSON_UNESCAPED_SLASHES flag constant in PHP.
*
* @link http://stackoverflow.com/a/10210433/367456
*/

$url = 'http://www.example.com/';

echo json_encode($url), "\n";

echo json_encode($url, JSON_UNESCAPED_SLASHES), "\n";

Example Output:

"http:\/\/www.example.com\/"
"http://www.example.com/"

php json_encode() automatically added slashes before slashes

Why you do like this? Is totaly bad thing adding slashes manualy.

You can just generate JSON using arrays like:

$arr=array();

$arr['buttonText']="Large Button";
$arr['campName']="Large's Button Test";
$arr['buttonSize']=1;

echo json_encode($arr);

Just use json_encode() to store values and json_decode() to get values.

Here is diferent aproach:

$arr=array(
'buttonText'=>"Large Button",
'campName'=>"Large's Button Test",
'buttonSize'=>1,
);

echo json_encode($arr);

JSON ENCODE - Manual

JSON DECODE - Manual

json_encode() adding slashes automaticaly and json_decode() remove it. You don't need to think about that. Just don't worry and be happy.

Why does json_encode escape the forward slash ( / )?

It is just as a safety net for the </endtag> structure for JSON embedded in <script> elements in HTML documents.

It has no other significance.

How to remove backslash on json_encode() function?

json_encode($response, JSON_UNESCAPED_SLASHES);

Why is json_encode adding backslashes?

Can anyone tell me why json_encode adds slashes?

Forward slash characters can cause issues (when preceded by a < it triggers the SGML rules for "end of script element") when embedded in an HTML script element. They are escaped as a precaution.

Because when I try do use jQuery.parseJSON(response); in my js script, it returns null. So my guess it has something to do with the slashes.

It doesn't. In JSON "/" and "\/" are equivalent.

The JSON you list in the question is valid (you can test it with jsonlint). Your problem is likely to do with what happens to it between json_encode and parseJSON.

JSON: why are forward slashes escaped?

JSON doesn't require you to do that, it allows you to do that. It also allows you to use "\u0061" for "A", but it's not required, like Harold L points out:

The JSON spec says you CAN escape forward slash, but you don't have to.

Harold L answered Oct 16 '09 at 21:59

Allowing \/ helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings, like Seb points out:

This is because HTML does not allow a string inside a <script> tag to contain </, so in case that substring's there, you should escape every forward slash.

Seb answered Oct 16 '09 at 22:00 (#1580667)

Some of Microsoft's ASP.NET Ajax/JSON API's use this loophole to add extra information, e.g., a datetime will be sent as "\/Date(milliseconds)\/". (Yuck)



Related Topics



Leave a reply



Submit