写入文件时停止PHP转义JSON字符串

时间:2011-09-14 18:38:27

标签: php jquery ajax json escaping

我正在尝试使用JQuery和PHP保存对JSON文件的更改,但似乎我的PHP脚本在保存JSON时转义字符,这意味着我无法再读回来。

我使用以下代码将JSON对象('family')传递给save.php:

function saveChanges() {
    $.ajax({
        type: "POST",
         url: "save.php",
         data: {
            data: JSON.stringify(family)
         },


         success: function(msg){
             console.log(data);
   }
 });
    }

然后save.php使用以下代码

将JSON数据写入armstrong.json
<?php

$data = $_POST["data"];
echo $data;
$filename = 'armstrong.json';

if (is_writable($filename)) {
    if (!$handle = fopen($filename, "w")) {
         echo "Cannot open file ($filename)";
         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";
}

?>

但是文件的写法如下:

{\"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\"}]}

任何想法如何阻止它逃脱角色? JSON文件应如下所示

{
            "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"
                }

            ]
}

2 个答案:

答案 0 :(得分:4)

也许你在php.ini中打开了magic quotes。你应该把它们关掉。这可以解释逃避

编辑 - 如果您需要了解有关魔术引语的更多信息,请阅读here。魔术引用很糟糕,如果你有权访问你的php.ini你应该关掉它们

答案 1 :(得分:1)

您已启用magic_quotes_gpc并且在执行$data = $_POST['data']时已存在斜杠。

请参阅此答案:Slash appended to all my posts

BTW你可以用file_put_contents

替换fopen / fwrite / fclose