Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
51
rated 0 times [  56] [ 5]  / answers: 1 / hits: 110245  / 12 Years ago, tue, july 3, 2012, 12:00:00

I'm using the tag-it library for jquery to make a tagging system (a bit like the stackoverflow one).



After the user types his tags the library returns a javascript array that I want to save in a MySQL database. I didn't find a serialize and unserialize function in javascript.



Before coding my own function I'd like to make sure I'm not reinventing the wheel here. It seems crazy that there is no native way to save an array to a database and then use it again.



tl;dr => how can I save a javascript array in a MySQL database to reuse it later ?


More From » jquery

 Answers
8

You can use JSON.stringify() (MDN docu) and JSON.parse() (MDN docu) for converting a JavaScript object into a string representation to store it inside a database.



var arr = [ 1, 2, 3 ];

var serializedArr = JSON.stringify( arr );
// [1, 2, 3]

var unpackArr = JSON.parse( serializedArr );
// identical array to arr


If your backend is written in PHP, there are similar methods to work with JSON strings there: json_encode() (PHP docu) and json_decode() (PHP docu).



Most other languages offer similar functionalities for JSON strings.


[#84498] Monday, July 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kelleyamiahk

Total Points: 216
Total Questions: 113
Total Answers: 119

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;