如何使用jQuery创建JSON对象

use*_*958 78 arrays json

我有一个以下格式的JSON对象:

temp:[
        {
           test:'test 1',
           testData:  [ 
                       {testName: 'do',testId:''}
                         ],
           testRcd:'value'                             
        },
        {
            test:'test 2',
           testData:  [
                            {testName: 'do1',testId:''}
                         ],
           testRcd:'value'                           
        }
      ],
Run Code Online (Sandbox Code Playgroud)

如何在jquery中为上述格式创建JSON对象.我想创建一个动态JSON对象.

Tim*_*Tim 205

只需将您的数据放入对象中,如下所示:

var myObject = new Object();
myObject.name = "John";
myObject.age = 12;
myObject.pets = ["cat", "dog"];
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式进行字符串化

var myString = JSON.stringify(myObject);
Run Code Online (Sandbox Code Playgroud)

你不需要jQuery.这是纯粹的JS.

  • 上述注释的一个凸点:要替换任何静态值,例如`.name`` .age`或`.pets`,只需用包含在方括号中的变量替换它,包括点.示例:`myObject [cssProp] = cssVal;`然后,在Object中将使用这两个css变量的值.这是一个jsFiddle:[http://fiddle.jshell.net/arttronics/rucjtbza/](http://fiddle.jshell.net/arttronics/rucjtbza/) (3认同)
  • 是否可以动态创建索引名称.例如:var name = $('#myname').val(); myObject.name ="john"//这里的名字索引将来自输入框dinamaclly. (2认同)

Den*_*ret 31

"JSON对象"没有意义:JSON是基于Javascript对象声明结构的交换格式.

如果要将javascript对象转换为json字符串,请使用JSON.stringify(yourObject);

如果你想创建一个javascript对象,只需这样做:

var yourObject = {
          test:'test 1',
          testData: [ 
                {testName: 'do',testId:''}
          ],
          testRcd:'value'   
};
Run Code Online (Sandbox Code Playgroud)

  • 感谢评论家们,我意识到我在阅读完这篇文章之后对这些东西的工作方式有了根本的误解,并且问我自己关于我正在研究的项目的问题.我很确定我现在明白了,谢谢你的耐心等待. (3认同)
  • @delliottg我不是说它是一个有效的JSON.这个答案的要点是区分JSON和JS对象.尝试在JS解释器中运行dystroy代码,你会发现它运行得很好. (2认同)

smu*_*nd2 5

我相信他要求将新的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.
Run Code Online (Sandbox Code Playgroud)

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
?>
Run Code Online (Sandbox Code Playgroud)