Javascript 将数组转换为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2295496/
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
Convert array to JSON
提问by dotty
I have an Array var cars = [2,3,..]which holds a few integers.
I've added a few values to the array, but I now need to send this array to a page via jQuery's .getmethod. How can I convert it to a JSON object for sending?
我有一个var cars = [2,3,..]包含几个整数的数组。我已经向数组中添加了一些值,但我现在需要通过 jQuery 的.get方法将此数组发送到页面。如何将其转换为 JSON 对象进行发送?
回答by JonoW
Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
向后兼容脚本:https: //github.com/douglascrockford/JSON-js/blob/master/json2.js
And call:
并调用:
var myJsonString = JSON.stringify(yourArray);
Note:The JSON object is now part of most modern web browsers (IE 8 & above). See caniusefor full listing. Credit goes to: @Spudley for his comment below
注意:JSON 对象现在是大多数现代 Web 浏览器(IE 8 及更高版本)的一部分。有关完整列表,请参阅caniuse。归功于:@Spudley 在下面发表评论
回答by Stancho Stanchev
I made it that way:
我是这样做的:
if I have:
如果我有:
var jsonArg1 = new Object();
jsonArg1.name = 'calc this';
jsonArg1.value = 3.1415;
var jsonArg2 = new Object();
jsonArg2.name = 'calc this again';
jsonArg2.value = 2.73;
var pluginArrayArg = new Array();
pluginArrayArg.push(jsonArg1);
pluginArrayArg.push(jsonArg2);
to convert pluginArrayArg (which is pure javascript array) into JSON array:
将 pluginArrayArg(纯 javascript 数组)转换为 JSON 数组:
var jsonArray = JSON.parse(JSON.stringify(pluginArrayArg))
回答by Eduardo Sganzerla
Wow, seems it got a lot easier nowadays... 3 ways you can do it:
哇,现在似乎变得容易多了……你可以通过 3 种方法来做到这一点:
json = { ...array };
json = Object.assign({}, array);
json = array.reduce((json, value, key) => { json[key] = value; return json; }, {});
回答by Frank Adrian
Or try defining the array as an object. (var cars = {};) Then there is no need to convert to json. This might not be practical in your example but worked well for me.
或者尝试将数组定义为对象。(var cars = {};) 那么就不用转json了。这在您的示例中可能不实用,但对我来说效果很好。
回答by Paulo Pedroso
I decided to use the json2 library and I got an error about “cyclic data structures”.
我决定使用 json2 库,但我收到了一个关于“循环数据结构”的错误。
I got it solved by telling json2 how to convert my complex object. Not only it works now but also I have included only the fields I need. Here is how I did it:
我通过告诉 json2 如何转换我的复杂对象来解决它。它不仅现在有效,而且我只包含了我需要的字段。这是我如何做到的:
OBJ.prototype.toJSON = function (key) {
var returnObj = new Object();
returnObj.devid = this.devid;
returnObj.name = this.name;
returnObj.speed = this.speed;
returnObj.status = this.status;
return returnObj;
}
回答by Vijay Bansal
One other way could be this:
另一种方式可能是这样的:
var json_arr = {};
json_arr["name1"] = "value1";
json_arr["name2"] = "value2";
json_arr["name3"] = "value3";
var json_string = JSON.stringify(json_arr);
回答by Kamil Kie?czewski
The shortest way I know to generate valid json from array of integers is
我知道从整数数组生成有效 json 的最短方法是
let json = `[${cars}]`
for more general object/array (without circular references) use JSON.stringify(cars)
对于更一般的对象/数组(没有循环引用)使用 JSON.stringify(cars)
let cars = [1,2,3]; cars.push(4,5,6);
let json = `[${cars}]`;
console.log(json);
console.log(JSON.parse(json)); // json validation
回答by Mahmoud ihmaid
If you have only 1 object like the one you asked, the following will work.
如果您只有 1 个像您询问的对象一样,则以下方法将起作用。
var x = [{'a':'b'}];
var b= JSON.stringify(x);
var c = b.substring(1,b.length-1);
JSON.parse(c);
回答by Harshal Lonare
because my array was like below: and I used .push function to create it dynamically
因为我的数组如下所示:我使用 .push 函数动态创建它
my_array = ["234", "23423"];
The only way I converted my array into json is
我将数组转换为 json 的唯一方法是
json = Object.assign({}, my_array);

