Stop PHP escaping JSON string when writing to file
I'm trying to save changes to a JSON file using JQuery and PHP, but it seems my PHP script is escaping the characters when it saves out the JSON, meaning I can't read it back in again.
I'm passing the JSON object ('family') to save.php using the following code:
function saveChanges() {
$.ajax({
type: "POST",
url: "save.php",
data: {
data: JSON.stringify(family)
},
success: function(msg){
console.log(data);
}
});
}
Then save.php writes the JSON data to armstrong.json with the following code
<?php
$data = $_POST["data"];
echo $data;
$filename = 'armstrong.json';
if (is_writable($filename)) {
if (!$handle = fopen($filename, "w")) {
echo "Cannot open file ($file开发者_JS百科name)";
exit;
}
if (fwrite($handle, parse_json($data)) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($data) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
However the file is being written out as follows:
{\"title\":\"Armstrong\",\"description\":\"The Armstrong Family\",\"patriarchID\":\"id1\",\"latestID\":8,\"members\":[{\"name\":\"Grandad\",\"id\":\"id1\",\"children\":[\"id2\",\"id3\"]},{\"name\":\"Dad\",\"id\":\"id2\",\"children\":[\"id4\",\"id5\",\"id6\",\"id7\"]},{\"name\":\"Uncle\",\"id\":\"id3\"},{\"name\":\"Child\",\"id\":\"id4\"},{\"name\":\"Child\",\"id\":\"id5\"},{\"name\":\"Child\",\"id\":\"id6\"},{\"name\":\"Child\",\"id\":\"id7\"},{\"name\":\"a\",\"id\":\"id8\"}]}{\"title\":\"Armstrong\",\"description\":\"The Armstrong Family\",\"patriarchID\":\"id1\",\"latestID\":9,\"members\":[{\"name\":\"Grandad\",\"id\":\"id1\",\"children\":[\"id2\",\"id3\"]},{\"name\":\"Dad\",\"id\":\"id2\",\"children\":[\"id4\",\"id5\",\"id6\",\"id7\"]},{\"name\":\"Uncle\",\"id\":\"id3\"},{\"name\":\"Child\",\"id\":\"id4\"},{\"name\":\"Child\",\"id\":\"id5\"},{\"name\":\"Child\",\"id\":\"id6\"},{\"name\":\"Child\",\"id\":\"id7\"},{\"name\":\"a\",\"id\":\"id8\"},{\"name\":\"a\",\"id\":\"id9\"}]}{\"title\":\"Armstrong\",\"description\":\"The Armstrong Family\",\"patriarchID\":\"id1\",\"latestID\":10,\"members\":[{\"name\":\"Grandad\",\"id\":\"id1\",\"children\":[\"id2\",\"id3\"]},{\"name\":\"Dad\",\"id\":\"id2\",\"children\":[\"id4\",\"id5\",\"id6\",\"id7\"]},{\"name\":\"Uncle\",\"id\":\"id3\"},{\"name\":\"Child\",\"id\":\"id4\"},{\"name\":\"Child\",\"id\":\"id5\"},{\"name\":\"Child\",\"id\":\"id6\"},{\"name\":\"Child\",\"id\":\"id7\"},{\"name\":\"a\",\"id\":\"id8\"},{\"name\":\"a\",\"id\":\"id9\"},{\"name\":\"a\",\"id\":\"id10\"}]}
Any ideas how I can stop it escaping the characters? The JSON file should look like this
{
"title" : "Armstrong",
"description" : "The Armstrong Family",
"patriarchID" : "id1",
"latestID" : 7,
"members" : [
{
"name" : "Grandad",
"id" : "id1",
"children": ["id2","id3"]
},
{
"name" : "Dad",
"id": "id2",
"children": ["id4","id5","id6","id7"]
},
{
"name" : "Uncle",
"id" : "id3"
},
{
"name" : "Child",
"id" : "id4"
},
{
"name" : "Child",
"id" : "id5"
},
{
"name" : "Child",
"id" : "id6"
},
{
"name" : "Child",
"id" : "id7"
}
]
}
Maybe you have magic quotes turned on in your php.ini. You should turn them off. This would explain the escaping
EDIT - if you need to know more about magic quotes read here. Magic quotes are bad, if you have access to your php.ini you should turn them off
You have magic_quotes_gpc
enabled and the slashes already exist at the time you do $data = $_POST['data']
.
See this answer: Slash appended to all my posts
BTW you could replace fopen/fwrite/fclose by file_put_contents
you have to decode your json then encode for simple but ugly example:
<?php
$json = file_get_contents('php://input');
$data = json_decode($json);
$json = json_encode(array($data));
echo $json;
?>
精彩评论