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

标签 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"
                }

            ]
}

最佳答案

也许你有magic quotes在你的 php.ini 中打开。你应该把它们关掉。这可以解释转义

编辑 - 如果您需要了解有关魔术引号的更多信息,请阅读 here 。魔术引号很糟糕,如果您有权访问 php.ini,则应该将其关闭

关于php - 写入文件时停止 PHP 转义 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7421299/

相关文章:

php - OOP MySQLi 连接

php - 如何从 git merge 中排除一个特定的提交(永远)

javascript - PHP服务器只接受部分图片,跨域

ajax - 从 ExtJS 请求到 node.js 时出现 CORS 问题。请求或响应 header 不正确?

php - 解决升级到 PHP 5.3 后显示 MySQL 数据库结果时不正确的字符编码

php - 在 PHP 中删除表上的行跨度

javascript - 为什么此 CSS 代码会在桌面 View 中 chop 内容?

javascript - 使用 $.get 加载弹出窗口 - Jquery Mobile

javascript - 有没有比纯 CSS 2 更好的、符合标准的方法来为网站制作下拉导航?

php - 在 PHP 中从数组中回显值时如何摆脱逗号 (,)