jQuery JSON 对象转字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3628164/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:46:42  来源:igfitidea点击:

JSON Object to String

jqueryjson

提问by James moore

How can I take a JSON object and convert it back into the original string format?

如何获取 JSON 对象并将其转换回原始字符串格式?

Thanks

谢谢

回答by rochal

You could use an excelent json2.jslibrary: https://github.com/douglascrockford/JSON-js

你可以使用一个优秀的json2.js库:https: //github.com/douglascrockford/JSON-js

Check out the JSON.stringifymethod.

检查JSON.stringify方法。

Edit:

编辑:

It seems like the librabry moved to github repo now: https://github.com/douglascrockford/JSON-js

似乎图书馆现在转移到了 github 存储库:https: //github.com/douglascrockford/JSON-js

回答by Silkster

Use the stringify method from Douglas Crockford's JSON object:

使用 Douglas Crockford 的 JSON 对象中的 stringify 方法:

http://www.json.org/js.html

http://www.json.org/js.html

This will show your object in an alert box:

这将在警告框中显示您的对象:

alert(JSON.stringify(myObject));

回答by aularon

when it's an object, it's a javascript object, then it gets encoded as a string, a string that is a javascript syntax that expresses the object json-encoded. (JSON: JavaScript Object Notation).

当它是一个对象时,它是一个 javascript 对象,然后它被编码为一个字符串,一个字符串是一个 javascript 语法,表示对象 json 编码。( JSON: JavaScript Object Notation).

However, these are the jquery built in function to parse json is jQuery.getJSON(retrieves the object out of the string).

但是,这些是 jquery 内置函数来解析 json is jQuery.getJSON(从字符串中检索对象)。

to do things the other way around check this: Serializing to JSON in jQuery

以相反的方式做事检查这个:Serializing to JSON in jQuery

回答by Bruno

There are a few jQuery plugins to do this. jQuery-jsonis one of them, which I've tried successfully.

有一些 jQuery 插件可以做到这一点。jQuery-json就是其中之一,我已经成功尝试过。