Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Object to string and back [duplicate]

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?

like image 968
Dmitrij Holkin Avatar asked Dec 01 '25 23:12

Dmitrij Holkin


1 Answers

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.

like image 137
hungndv Avatar answered Dec 04 '25 12:12

hungndv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!