Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  84] [ 6]  / answers: 1 / hits: 22581  / 9 Years ago, tue, july 7, 2015, 12:00:00

I need to convert Javascript object to string and then this string back to object.



Objects i get like that:



    var Checked = {};

// Hold all checkboxes
$('div.list input[type=radio]:checked, input[type=checkbox]:checked').each(function () {
var $el = $(this);
var name = $el.attr('name');
if (typeof (Checked[name]) === 'undefined') {
Checked[name] = [];
}
Checked[name].push($el.val());
});


I know how to do this with array by using join and split, but how to be with objects?
Now how to convert this object to string?
How to get back this string to object?


More From » jquery

 Answers
24

Here you are:





var object = {
1: [1, 2, {
3: 3
}]
};
var str = JSON.stringify(object);
console.log(str);
var obj = JSON.parse(str);
console.log(obj[1][2][3]);





Hope this helps.


[#65904] Saturday, July 4, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
charity

Total Points: 503
Total Questions: 98
Total Answers: 125

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
;