今天在Stack Overflow上看到一个帖子,如何把js对象转化为json字符串。
If I defined an object in JS with:
var j={"name":"binchen"};How can I convert the object to JSON? The output string should be:
'{"name":"binchen"}'我们排除IE6、7,可以用下面这个小方法来达到转换的目的。
All current browsers have native JSON support built in. So as long as you're not dealing with prehistoric browsers like IE6/7 you can do it just as easily as that:
var j={"name":"binchen"}; JSON.stringify(j); // '{"name":"binchen"}'
说白了:JSON.stringify()。