Tengo un objeto JSON en el siguiente formato:
temp:[
{
test:'test 1',
testData: [
{testName: 'do',testId:''}
],
testRcd:'value'
},
{
test:'test 2',
testData: [
{testName: 'do1',testId:''}
],
testRcd:'value'
}
],
¿Cómo puedo crear un objeto JSON en jquery para el formato anterior? Quiero crear un objeto JSON dinámico.

.name.ageo.petssimplemente reemplazarlo, incluido el punto, con una variable entre corchetes. Ejemplo:myObject[cssProp] = cssVal;Entonces, cualesquiera que sean los valores de esas dos variables css, se usarán en el objeto. Aquí hay un jsFiddle: http://fiddle.jshell.net/arttronics/rucjtbza/Un "objeto JSON" no tiene sentido: JSON es un formato de intercambio basado en la estructura de la declaración del objeto Javascript.
Si desea convertir su objeto javascript en una cadena json, use
JSON.stringify(yourObject);Si desea crear un objeto javascript, simplemente hágalo así:
var yourObject = { test:'test 1', testData: [ {testName: 'do',testId:''} ], testRcd:'value' };fuente
Creo que está pidiendo escribir el nuevo json en un directorio. Necesitará algo de Javascript y PHP. Entonces, para aprovechar las otras respuestas:
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 ?>fuente
Cómo agregar el valor del campo de entrada como json like
temp:[ { test:'test 1', testData: [ {testName: 'do',testId:''} ], testRcd:'value' }, { test:'test 2', testData: [ {testName: 'do1',testId:''} ], testRcd:'value' } ],fuente
JSONObjeto anidadovar data = { view:{ type: 'success', note:'Updated successfully', }, };Puede analizar esto
data.view.typeydata.view.noteJSONObjeto y matriz interiorvar data = { view: [ {type: 'success', note:'updated successfully'} ], };Puede analizar esto
data.view[0].typeydata.view[0].notefuente
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) { } });fuente
modelvariable. Esta pregunta es: "En JavaScript, ¿cómo puedo crear un objeto en tiempo de ejecución y representar ese objeto en notación JSON" , que su respuesta aún no muestra.