J’ai un object JSON au format ci-dessous:
temp:[ { test:'test 1', testData: [ {testName: 'do',testId:''} ], testRcd:'value' }, { test:'test 2', testData: [ {testName: 'do1',testId:''} ], testRcd:'value' } ],
Comment puis-je créer un object JSON dans jquery pour le format ci-dessus. Je veux créer un object JSON dynamic.
Il suffit de mettre vos données dans un object comme ceci:
var myObject = new Object(); myObject.name = "John"; myObject.age = 12; myObject.pets = ["cat", "dog"];
Ensuite, ficelez-le via:
var mySsortingng = JSON.ssortingngify(myObject);
Vous n’avez pas besoin de jQuery pour cela. C’est du pur JS.
Un “object JSON” n’a pas de sens: JSON est un format d’échange basé sur la structure de la déclaration d’object Javascript.
Si vous souhaitez convertir votre object javascript en chaîne json, utilisez JSON.ssortingngify(yourObject)
;
Si vous voulez créer un object javascript, faites-le simplement comme ceci:
var yourObject = { test:'test 1', testData: [ {testName: 'do',testId:''} ], testRcd:'value' };
Je crois qu’il demande à écrire le nouveau json dans un répertoire. Vous aurez besoin de Javascript et de PHP. Donc, pour reprendre les autres réponses:
script.js
var yourObject = { test:'test 1', testData: [ {testName: 'do',testId:''} ], testRcd:'value' }; var mySsortingng = 'newData='+JSON.ssortingngify(yourObject); //converts json to ssortingng and prepends the POST variable name $.ajax({ type: "POST", url: "buildJson.php", //the name and location of your php file data: mySsortingng, //add the converted json ssortingng 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
Objet JSON
nested
var data = { view:{ type: 'success', note:'Updated successfully', }, };
Vous pouvez parsingr ces data.view.type
et data.view.note
Objet JSON
et tableau interne
var data = { view: [ {type: 'success', note:'updated successfully'} ], };
Vous pouvez parsingr ce data.view[0].type
et data.view[0].note
Comment obtenir une valeur de champ d’entrée comme json comme
temp:[ { test:'test 1', testData: [ {testName: 'do',testId:''} ], testRcd:'value' }, { test:'test 2', testData: [ {testName: 'do1',testId:''} ], testRcd:'value' } ],
var model = {"Id": "xx", "Name":"Ravi"}; $.ajax({ url: 'test/set', type: "POST", data: model, success: function (res) { if (res != null) { alert("done."); } }, error: function (res) { } });