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

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

问题描述

有一个很好的方法来编码一个javascript对象作为JSON?

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

我有一个键值对的列表...其中的名称是从一个复选框,并且根据是否选中该框,该值为true或false:

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).

有人知道解决方案吗?谢谢!!

Does anyone know a solution? Thanks!!

推荐答案

我认为您可以使用JSON.stringify

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

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

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