好的,我正在使用 js 编写一个 Web 操作系统。我使用 JSON 作为文件系统。我已经在网上寻找有关 JSON 内容的教程大约一周了,但我找不到任何有关从网页编写 JSON 文件的内容。我需要在文件中创建新对象,而不是更改现有对象。到目前为止,这是我的代码:
{"/": {
"Users/": {
"Guest/": {
"bla.txt": {
"content":
"This is a test text file"
}
},
"Admin/": {
"html.html": {
"content":
"yo"
}
}
},
"bin/": {
"ls": {
"man": "Lists the contents of a directory a files<br/>Usage: ls"
},
"cd": {
"man": "Changes your directory<br/>Usage: cd <directory>"
},
"fun": {
"man": "outputs a word an amount of times<br/>Usage: fun <word> <times>"
},
"help": {
"man": "shows a list of commands<br/>Usage: help"
},
"clear": {
"man": "Clears the terminal<br/>Usage: clear"
},
"cat": {
"man": "prints content of a file<br/>Usage: cat <filename>"
}
},
"usr/": {
"bin/": {
},
"dev/": {
}
}
}}
我认为更好的解决方案是对 JSON 进行字符串化,使用 base64 编码进行编码,然后将其发送到可以保存此文件的服务器端脚本(例如 PHP 页面)。看:
var json = JSON.stringify(myJson);
var encoded = btoa(json);
您可以使用ajax来发送:
var xhr = new XMLHttpRequest();
xhr.open('POST','myServerPage.php',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send('json=' + encoded);
在服务器端:
$decoded = base64_decode($_POST['json'])
$jsonFile = fopen('myJson.json','w+');
fwrite($jsonFile,$decoded);
fclose($jsonFile);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)