Good way to serialize a list? - Javascript / AJAX

I just wanted to ask about it, since stones always appear in stackoverflow :)

I have the following list:

list1 = [['command','arg1','arg2'], ['command2','arg1'], ... ]

How would you recommend converting it to a string to pass as an ONE GET argument?

eg.

http://webgame_site.com/command_list/?data=...

What I'm doing right now is separating the lists with commas , ;, but I don’t like this idea, because the method will break if I decide to enter them in strings.

I try to be as compact as possible.


One of my ideas is to encode the list in base64:

[['command','arg1','arg2'], ['command2','arg1']]
=> "W1snY29tbWFuZCcsJ2FyZzEnLCdhcmcyJ10sWydjb21tYW5kMicsJ2FyZzEnXV0="

which is less than URIencode


Any ideas? :)

+3
source share
2 answers

json, , encodeURI.

var list1 = [['command','arg1','arg2'], ['command2','arg1']];
var encoded = encodeURI(JSON.stringify(list1));

alert(encoded);

base64:

var list1 = [['command','arg1','arg2'], ['command2','arg1']];
var encoded = btoa(JSON.stringify(list1));

alert(encoded);
alert(atob(encoded));
+4
+2

Source: https://habr.com/ru/post/1781571/


All Articles