Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  31] [ 2]  / answers: 1 / hits: 29482  / 12 Years ago, mon, january 21, 2013, 12:00:00

I have an associative array as follows:



var AssocArray = { id:0, folder:'Next', text:'Apple' };


Now I need to store this in a database, so I figure I would just convert this into a string, store it in the database and then pull it out of the database and put it back into a javascript array later on.



The catch is that the actual # of items, and the array variables will be different every time (hence why I wanted to store it as one long string instead).



What’s the best way to convert this associative array into a string, and then also vice versa, how to convert a string into an associative array?


More From » arrays

 Answers
24

There is nothing better than JSON for it:



var str = JSON.stringify(obj);
// >> {id:0,folder:Next,text:Apple}

var obj = JSON.parse(str);
// >> Object({ id: 0, folder: Next, text: Apple })

[#80738] Friday, January 18, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeremiahianx

Total Points: 629
Total Questions: 106
Total Answers: 112

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;