Javascript 如何将 JSON 转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10269469/
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
How to convert JSON to string?
提问by Danny Fox
Possible Duplicate:
Convert JS object to JSON string
可能的重复:
将 JS 对象转换为 JSON 字符串
I have a JSON object in JS, and I would like to convert it to string. Is it a function for this?
我在 JS 中有一个 JSON 对象,我想将它转换为字符串。这是一个功能吗?
Thanks in advance,
提前致谢,
回答by Vitalii Petrychuk
Convert a value to JSON, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
将值转换为 JSON,如果指定了替换器函数,则可以选择替换值,或者如果指定了替换器数组,则可以选择仅包含指定的属性。
回答by Sampson
You can use the JSON stringify
method.
您可以使用JSONstringify
方法。
JSON.stringify({x: 5, y: 6}); // '{"x":5,"y":6}' or '{"y":6,"x":5}'
There is pretty good support for this across the board when it comes to browsers, as shown on http://caniuse.com/#search=JSON. You will note, however, that versions of IE earlier than 8 do not support this functionality natively.
如http://caniuse.com/#search=JSON所示,在浏览器方面有很好的全面支持。但是,您会注意到 IE 8 之前的版本本身不支持此功能。
If you wish to cater to those users as well you will need a shim. Douglas Crockford has provided his own JSON Parseron github.
如果您也想迎合这些用户,您将需要一个垫片。Douglas Crockford在 github 上提供了他自己的JSON 解析器。
回答by grifos
Try to Use JSON.stringify
尝试使用 JSON.stringify
Regards
问候