简体   繁体   中英

Object to string, and vice-versa

Say I had the object:

var object = {
    1: [2,5,"hi"],
    hi: {hihi: 1}
};

How would I convert that to a string, and then back, preserving all the information? I would need this to work for a big object, with the values themselves being objects.

This is not a duplicate, the others didn't involve getting the object back.

Below is a live demo of how you can convert an object to a string and back using JSON.stringify() and JSON.parse() .

Open your browser console and you can see that all attributes are preserved after the conversion to a string and back.

 var object = { 1: [2,5,"hi"], hi: {hihi: 1} }; console.log(object); var strobj = JSON.stringify(object); console.log(strobj); var unstrobj = JSON.parse(strobj); console.log(unstrobj); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM