Serializing and unserializing an array in javascript

一曲冷凌霜 提交于 2019-12-02 22:13:00

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.

You can use JavaScript Object Notation(JSON) format.

Javascript supports these methods:

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. )

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!