82

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

temp:[
        {
           test:'test 1',
           testData:  [ 
                       {testName: 'do',testId:''}
                         ],
           testRcd:'value'                             
        },
        {
            test:'test 2',
           testData:  [
                            {testName: 'do1',testId:''}
                         ],
           testRcd:'value'                           
        }
      ],

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

4

6 回答 6

215

只需将您的数据放入这样的对象中:

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

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

var myString = JSON.stringify(myObject);

为此,您不需要 jQuery。它是纯 JS。

于 2012-06-18T07:15:46.133 回答
30

“JSON 对象”没有意义:JSON是一种基于 Javascript 对象声明结构的交换格式。

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

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

var yourObject = {
          test:'test 1',
          testData: [ 
                {testName: 'do',testId:''}
          ],
          testRcd:'value'   
};
于 2012-06-18T07:15:26.273 回答
5

我相信他要求将新的 json 写入目录。您将需要一些 JavascriptPHP。因此,要捎带其他答案:

脚本.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
?>
于 2013-02-12T21:37:53.953 回答
1

如何将输入字段值附加为json

temp:[
        {
           test:'test 1',
           testData:  [ 
                       {testName: 'do',testId:''}
                         ],
           testRcd:'value'                             
        },
        {
            test:'test 2',
           testData:  [
                            {testName: 'do1',testId:''}
                         ],
           testRcd:'value'                           
        }
      ],
于 2018-07-17T05:14:54.793 回答
0

嵌套JSON对象

var data = {
        view:{
            type: 'success', note:'Updated successfully',
        },
    };

你可以解析这个data.view.typedata.view.note

JSON对象和内部数组

var data = {
          view: [ 
                {type: 'success', note:'updated successfully'}
          ],  
     };

你可以解析这个data.view[0].typedata.view[0].note

于 2017-03-07T06:33:31.167 回答
-1
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) {

                        }
                    });
于 2015-12-24T15:01:39.447 回答