Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

javascript - 将数组转换为JSON(Convert array to JSON)

I have an Array var cars = [2,3,..] which holds a few integers.

(我有一个数组var cars = [2,3,..] ,其中包含一些整数。)

I've added a few values to the array, but I now need to send this array to a page via jQuery's .get method.

(我已经向数组添加了一些值,但是现在我需要通过jQuery的.get方法将此数组发送到页面。)

How can I convert it to a JSON object for sending?

(如何将其转换为JSON对象进行发送?)

  ask by dotty translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(注意: JSON对象现在是大多数现代Web浏览器(IE 8及更高版本)的一部分。)

See caniuse for full listing.

(有关完整列表,请参见caniuse)

Credit goes to: @Spudley for his comment below

(感谢:@Spudley在下面的评论)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...