写入文件时停止 PHP 转义 JSON 字符串
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您开启了魔法引号在你的 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
您已启用
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
你必须解码你的 json 然后编码简单但丑陋的例子:
you have to decode your json then encode for simple but ugly example: