MyTetra Share
Делитесь знаниями!
How do I encode a JavaScript object as JSON?
Время создания: 16.07.2018 10:56
Текстовые метки: javascript json encode decode
Раздел: Javascript
Запись: Velonski/mytetra-database/master/base/1531720601dz993fdabm/text.html на

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.checked; });

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?

javascript jquery json checkbox hashtable

shareimprove this question

edited Jul 23 '17 at 15:50

Brett DeWoody


asked Jun 6 '12 at 18:26

daniel langer



There's no such thing as a json object. Are you trying to serialize values into json, or are you trying to pass values into a JavaScript object? – Adam Rackis Jun 6 '12 at 18:34

I want to be able to create a json file to store the values of the checkboxes so the users choices are saved in a cookie. I am new to json so I don't know whicH i want – daniel langer Jun 6 '12 at 18:36


possible duplicate of storing and retrieving json objects to / from a cookie, serialize form to json and store in the cookie and How do I store this JSON object as a cookie and than read it in vanilla javascript? – Bergi Jun 6 '12 at 19:03

IE7 and below need the JSON2.js library and do not support this API natively. – Ritsaert Hornstra Mar 13 '13 at 15:22

add a comment

2 Answers

active oldest votes

up vote


down vote


I think you can use JSON.stringify:

// after your each loop


shareimprove this answer

edited Feb 28 '13 at 17:38



answered Jun 6 '12 at 18:31




I put this in an alert() but nothing appears – daniel langer Jun 6 '12 at 18:33

@daniellanger - based on your comment, this is the answer. You'll need to do some debugging to see why things aren't showing up – Adam Rackis Jun 6 '12 at 18:37

so how can I save this to a file/ store it as a cookie? – daniel langer Jun 6 '12 at 18:39


IE 10 is giving: JavaScript runtime error: 'JSON' is undefined – Matthew Lock Mar 22 '13 at 6:27


The native JSON methods are supported by IE8 and above. That being said, in IE they are only available in Standards Mode. – Boaz May 14 '14 at 13:13

show 3 more comments

up vote


down vote

All major browsers now include native JSON encoding/decoding.

// To encode an object (This produces a string)

var json_str = JSON.stringify(myobject);

// To decode (This produces an object)

var obj = JSON.parse(json_str);

Note that only valid JSON data will be encoded. For example:

var obj = {'foo': 1, 'bar': (function (x) { return x; })}

JSON.stringify(obj) // --> "{\"foo\":1}"

Valid JSON types are: objects, strings, numbers, arrays, true, false, and null.

Some JSON resources:

JSON on Mozilla Developer Network

JSON on Wikipedia

shareimprove this answer

edited Jun 6 '12 at 19:14

answered Jun 6 '12 at 18:48



Is my hash valid JSON data? – daniel langer Jun 6 '12 at 19:10


Well, they are called "objects" in javascript. But yes, boolean values can be represented in JSON

Так же в этом разделе:
MyTetra Share v.0.55
Яндекс индекс цитирования