MyTetra Share
Делитесь знаниями!
How do I encode a JavaScript object as JSON?
Время создания: 16.07.2018 10:56
Текстовые метки: javascript json encode decode
Раздел: Javascript

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

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

26.4k1583123

asked Jun 6 '12 at 18:26


daniel langer

6252916

1

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

3

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. caniuse.com/json – Ritsaert Hornstra Mar 13 '13 at 15:22

add a comment

2 Answers

active oldest votes

up vote

103

down vote

accepted

I think you can use JSON.stringify:


// after your each loop

JSON.stringify(values);

shareimprove this answer

edited Feb 28 '13 at 17:38


Alexander

19.7k74566

answered Jun 6 '12 at 18:31


mimiz

1,6481913

1

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

2

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

1

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

23

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


vezult

4,3741736

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

1

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

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