Javascript 将数组转换为 Json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8835042/
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-08-24 07:44:12  来源:igfitidea点击:

Convert Array to Json

javascriptjqueryhtmlajaxjson

提问by sachin

Possible Duplicate:
Serializing to JSON in jQuery
Convert Object to JSON string

可能的重复:
在 jQuery 中序列化为 JSON
将对象转换为 JSON 字符串

I want to convert array to json object in js or jquery. Is there any function is available or lib

我想在 js 或 jquery 中将数组转换为 json 对象。是否有任何可用的函数或lib

Thanks in advance

提前致谢

采纳答案by ThiefMaster

In modern browsers you can simply use JSON.stringify(someJSObject)to convert any JavaScript object (that includes arrays) to a JSON string.

在现代浏览器中,您可以简单JSON.stringify(someJSObject)地将任何 JavaScript 对象(包括数组)转换为 JSON 字符串。

To ensure it also works in less modern browsers, include https://github.com/douglascrockford/JSON-js/blob/master/json2.json your page.

为确保它也适用于不太现代的浏览器,在您的页面上包含https://github.com/douglascrockford/JSON-js/blob/master/json2.js

回答by ladar

How about

怎么样

var jsonString = JSON.stringify(myArray);