开发者

How do i convert a JS object to JSON?

开发者 https://www.devze.com 2023-01-06 05:53 出处:网络
How do I convert a JS object to a JSON string? var开发者_如何学Go o = { name: \"a\", id:5}; var sz = //???

How do I convert a JS object to a JSON string?

var开发者_如何学Go o = { name: "a", id:5};
var sz = //???
alert('The json will look like ' + sz);

I would like to do it natively if possible, or using jQuery if not.


You are looking for the JSON.stringify method:

var sz = JSON.stringify(o);

This method is part of the ECMAScript 5 Standard, and almost every browser includes it.

For older browsers and IE < 8, you can include the json2.js file.

0

精彩评论

暂无评论...
验证码 换一张
取 消