Serializing and unserializing an array in javascript

后端 未结 3 1856
无人共我
无人共我 2021-02-02 09:13

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 arra

相关标签:
3条回答
  • 2021-02-02 09:54

    You can use JavaScript Object Notation(JSON) format.

    Javascript supports these methods:

    • JSON.stringify -> serializes object to string

    • JSON.parse -> deserializes object from string

    0 讨论(0)
  • 2021-02-02 09:58

    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.

    0 讨论(0)
  • 2021-02-02 10:03

    How about just JSONing it?

    var arr = [1,2,3];
    var arrSerialized = JSON.stringify(arr);
    ...
    
    var arrExtracted = JSON.parse(arrSerialized);
    

    By the way, JSON is often used for serializing in some other languages, even though they have their own serializing functions. )

    0 讨论(0)
提交回复
热议问题