How to Remove Backslash on JSON_Encode() Function

How to remove backslash on json_encode() function?

json_encode($response, JSON_UNESCAPED_SLASHES);

remove backslash from json string

You can try the following way to achieve what you need,

$jsonString = '\"[\n      {\n        \"id\": \"1\",\n        \"mid\": \"1\",\n        \"num\": \"1\",\n        \"type\": \"wgp\",\n        \"time_changed\": \"time\",\n        \"username\": \"aaa\"\n      }\n    ]\"';

$jsonString = str_replace('\\n', '', $jsonString); // First remove the new line characters in the json string

$jsonString = str_replace('\\', '', $jsonString); // Replace backslash with empty string

echo preg_replace('!\s+!', ' ', $jsonString); // Replace multiple spaces with one

So the output is,

"[ { "id": "1", "mid": "1", "num": "1", "type": "wgp", "time_changed":
"time", "username": "aaa" } ]"

json_encode() escaping forward slashes

is there a way to disable it?

Yes, you only need to use the JSON_UNESCAPED_SLASHES flag (PHP 5.4+).

!important read before: https://stackoverflow.com/a/10210367/367456 (know what you're dealing with - know your enemy: DO NOT USE in web/html context - CLI, unless CGI, might be fine thought, if they think they need it in JSON HTTP context for readability purposes, they have a different problem)

json_encode($str, JSON_UNESCAPED_SLASHES);

If you don't have PHP 5.4 at hand (you certainly already asserted the warning above), 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/"

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.

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.



Related Topics



Leave a reply



Submit