Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  107] [ 1]  / answers: 1 / hits: 25497  / 11 Years ago, mon, june 10, 2013, 12:00:00

Ok, so I am programming a web operating system using js. I am using JSON for the file system. I have looking online for tutorials on JSON stuff for about a week now, but I cannot find anything on writing JSON files from a web page. I need to create new objects in the file, not change existing ones. Here is my code so far:



{/: {
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/: {

}
}
}}

More From » json

 Answers
10

I think the better solution is to stringify your JSON, encode with base64 encoding and then send it to a server-side script (a PHP page, for instance) which could save this file. See:



var json = JSON.stringify(myJson);
var encoded = btoa(json);


You can use ajax for sending:



var xhr = new XMLHttpRequest();
xhr.open('POST','myServerPage.php',true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send('json=' + encoded);


And in the server-side:



$decoded = base64_decode($_POST['json'])
$jsonFile = fopen('myJson.json','w+');
fwrite($jsonFile,$decoded);
fclose($jsonFile);

[#77713] Saturday, June 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaliyahcynthiac

Total Points: 91
Total Questions: 94
Total Answers: 119

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
;