将 javascript 对象序列化为 json 并返回
我正在使用 jQuery post 方法将一些数据发送到服务器。我不想自己构建 JSON 字符串,而是想简单地使用 JavaScript 对象。当我得到返回字符串(JSON 格式)时,我想自动构造一个相应的 JavaScript 对象。
这可能吗?
I am using to a jQuery post method to send some data to a server. Rather than constructing the JSON string myself, I would like to simply use a JavaScript object. When I get the return string (in JSON) I would like to automatically construct a corresponding JavaScript object.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 JSON2 文档
示例:
Checkout
JSON.stringify()
andJSON.parse()
in JSON2 documentationExample:
是的。
如果有 JSON 对象,您可以使用 :
将对象转换为 JSON 字符串。
您还可以将字符串转换为对象,
以确保 JSON 可用,您可以包含此文件 https://github.com /douglascrockford/JSON-js
Yes.
If the JSON object is available, you can use :
to transform an object into JSON string.
You can also convert a string into an object with
To be sure that JSON is available, you can include this file https://github.com/douglascrockford/JSON-js
您应该使用 Douglas Crockford 的 JSON2 库。
这样,您可以:
或者
you should use Douglas Crockford's JSON2 library.
That way, you could:
or
如果您使用 jQuery.getJSON 您不必关心字符串化和解析 json、jquery为你做。
If you use jQuery.getJSON you don't have to care about stringifying and parsing json, jquery does it for you.