Я считаю, что он просит написать новый json в каталог. Вам понадобятся некоторые Javascript и PHP. Таким образом, чтобы поросенок обратно от других ответов:
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, вы можете объяснить свой откат к моему редактировать? Если вы запустите объект через [JSONLint] (http://jsonlint.com/), он будет помечен как недопустимый (левые ключи должны быть двойными). Я не утверждаю, что вы ошибаетесь, я хочу узнать, почему вы считаете, что это действительно JSON, потому что это может быть то, что я не понимаю. Если вы запускаете мою версию с помощью одного и того же валидатора, она возвращается как действительный JSON. – delliottg
@delliottg Не используйте проверку подлинности JSON для проверки JavaScript. Пожалуйста, прочтите мой ответ еще раз. –
@delliottg Я не говорю, что это действительный JSON. Целью этого ответа является разграничение JSON объекта JS. Попробуйте запустить код dystroy в интерпретаторе JS, и вы увидите, что он работает отлично. –