如何将 JavaScript 对象编码为 JSON? [英] How do I encode a JavaScript object as JSON?

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

问题描述

是否有将 JavaScript 对象编码为 JSON 的好方法?

Is there a good way to encode a JavaScript object as JSON?

我有一个键值对列表...其中名称来自复选框,并且根据是否选中该框,该值为真或假:

I have a list of key value pairs...where the name is from a checkbox, and the value is either true or false based on whether the box is checked or not:

var values = {};
$('#checks :checkbox').each(function() { values[this.name]=this.checked; }); 

我想将这些值传递给一个 JSON 对象,因此存储到一个 cookie 中以呈现一个表(将根据用户检查的内容添加列).

I want to pass these values into a JSON object so store into a cookie to render a table (Columns will be added according to what the user checks off).

有人知道解决办法吗?

推荐答案

我认为你可以使用 JSON.stringify:

I think you can use JSON.stringify:

// after your each loop
JSON.stringify(values);

这篇关于如何将 JavaScript 对象编码为 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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