将对象转换为JSON字符串 [英] Convert Object to JSON string

查看:126
本文介绍了将对象转换为JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery.parseJSON('{"name":"John"}')将字符串表示形式转换为对象 但我要相反.对象将转换为JSON字符串 我有一个链接 http://www.devcurry.com /2010/03/convert-javascript-object-to-json.html 但是它需要json2.js来做吗jQuery有一个本机方法可以做到这一点?

jQuery.parseJSON('{"name":"John"}') converts string representation to object but I want the reverse. Object is to be converted to JSON string I got a link http://www.devcurry.com/2010/03/convert-javascript-object-to-json.html but it need to have json2.js do jQuery has a native method to do this?

推荐答案

jQuery只会在调用本机浏览器方法window.JSON.parse()之前进行一些正则表达式检查.如果不可用,它将使用eval()或更确切地说是new Function()来创建Javascript对象.

jQuery does only make some regexp checking before calling the native browser method window.JSON.parse(). If that is not available, it uses eval() or more exactly new Function() to create a Javascript object.

JSON.parse()的反面是JSON.stringify(),它将Javascript对象序列化为字符串. jQuery没有它自己的功能,您必须使用浏览器的内置版本或 http:/中的json2.js /www.json.org

The opposite of JSON.parse() is JSON.stringify() which serializes a Javascript object into a string. jQuery does not have functionality of its own for that, you have to use the browser built-in version or json2.js from http://www.json.org

JSON.stringify()在所有主流浏览器中都可用,但是要与旧版浏览器兼容,您仍然需要该备用.

JSON.stringify() is available in all major browsers, but to be compatible with older browsers you still need that fallback.

这篇关于将对象转换为JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆