Credo che stia chiedendo di scrivere il nuovo json in una directory. Avrai bisogno di alcuni Javascript e PHP. Quindi, per piggy back fuori le altre risposte:
script.js
var yourObject = {
test:'test 1',
testData: [
{testName: 'do',testId:''}
],
testRcd:'value'
};
var myString = 'newData='+JSON.stringify(yourObject); //converts json to string and prepends the POST variable name
$.ajax({
type: "POST",
url: "buildJson.php", //the name and location of your php file
data: myString, //add the converted json string to a document.
success: function() {alert('sucess');} //just to make sure it got to this point.
});
return false; //prevents the page from reloading. this helps if you want to bind this whole process to a click event.
buildJson.php
<?php
$file = "data.json"; //name and location of json file. if the file doesn't exist, it will be created with this name
$fh = fopen($file, 'a'); //'a' will append the data to the end of the file. there are other arguemnts for fopen that might help you a little more. google 'fopen php'.
$new_data = $_POST["newData"]; //put POST data from ajax request in a variable
fwrite($fh, $new_data); //write the data with fwrite
fclose($fh); //close the dile
?>
@guillaumealgis, puoi spiegare la tua rollback alla mia modifica? Se si esegue l'oggetto tramite [JSONLint] (http://jsonlint.com/) viene contrassegnato come non valido (i tasti a sinistra devono essere citati due volte). Non sto sostenendo che ti sbagli, voglio sapere perché credi che sia un JSON valido perché potrebbe essere qualcosa che non capisco. Se si esegue la mia versione tramite lo stesso validatore, viene restituito come JSON valido. – delliottg
@delliottg Non utilizzare un validatore JSON per convalidare JavaScript. Per favore leggi di nuovo l'inizio della mia risposta. –
@delliottg Non sto dicendo che è un JSON valido. Il punto di questa risposta è di differenziare JSON un oggetto JS. Prova a eseguire il codice dystroy in un interprete JS e vedrai che funziona alla perfezione. –