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?
Forward slash characters can cause issues (when preceded by aCan anyone tell me why json_encode adds slashes?
<
it triggers the SGML rules for "end of script element") when embedded in an HTML script element. They are escaped as a precaution.It doesn't. In JSONBecause 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.
"/"
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 - ManualJSON 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
How to Require PHP Files Relatively (At Different Directory Levels)
Php: How to See "Invisible" Characters Like \N
How to Use a Class Method as a Callback Function
PHP MySQL Group by to Get Latest Record, Not First Record
Remove "(Optional)" Text from Checkout Fields in Woocommerce 3.4+
Replacing Backslashes with Forward Slashes with Str_Replace() in PHP
Bool Parameter from Jquery Ajax Received as Literal String "False"/"True" in PHP
Count How Often a Particular Value Appears in an Array
SQL - Insert and Catch the Id Auto-Increment Value
Reverse Letters in Each Word of a String Without Using Native Splitting or Reversing Functions
PHP Date Function Seven Days Previous
Find Windows 32 or 64 Bit Using PHP
PHP Date Function Seven Days Previous
PHP MySQL Charset Utf8 Problems
How to Put the Results of a MySQLi Prepared Statement into an Associative Array