如何使用 jQuery 创建 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11078324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:24:00  来源:igfitidea点击:

How to create JSON object using jQuery

jsonarrays

提问by user1462958

I have a JSON object in below format:

我有以下格式的 JSON 对象:

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

How can i create JSON object in jquery for above format. I want to create a dynamic JSON object.

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

回答by Tim

Just put your data into an Object like this:

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

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

Afterwards stringify it via:

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

var myString = JSON.stringify(myObject);

You don't need jQuery for this. It's pure JS.

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

回答by Denys Séguret

A "JSON object" doesn't make sense : JSONis an exchange format based on the structure of Javascript object declaration.

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

If you want to convert your javascript object to a json string, use JSON.stringify(yourObject);

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

If you want to create a javascript object, simply do it like this :

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

var yourObject = {
          test:'test 1',
          testData: [ 
                {testName: 'do',testId:''}
          ],
          testRcd:'value'   
};

回答by smulholland2

I believe he is asking to write the new json to a directory. You will need some Javascript andPHP. So, to piggy back off the other answers:

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

script.js

脚本.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

构建Json.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
?>

回答by engrmukul

How to get append input field value as json like

如何像json一样获取附加输入字段值

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

回答by Thamaraiselvam

Nested JSONobject

嵌套JSON对象

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

You can parse this data.view.typeand data.view.note

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

JSONObject and inside Array

JSON对象和内部数组

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

You can parse this data.view[0].typeand data.view[0].note

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

回答by Ravi Anand

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) {

                        }
                    });