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

发布于 2024-12-04 17:48:35 字数 3751 浏览 0 评论 0原文

我正在尝试使用 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"
                }

            ]
}

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 ($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";
}

?>

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

            ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友欢 2024-12-11 17:48:35

也许您开启了魔法引号在你的 php.ini 中。你应该把它们关掉。这将解释转义

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

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

对不⑦ 2024-12-11 17:48:35

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

请参阅此答案:斜杠附加到我的所有帖子

顺便说一句可以用 file_put_contents

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

缱绻入梦 2024-12-11 17:48:35

你必须解码你的 json 然后编码简单但丑陋的例子:

<?php
$json = file_get_contents('php://input');
$data = json_decode($json);
$json = json_encode(array($data));

echo $json;

?>

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;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文