[英]Good way to serialize a list? - Javascript/AJAX
just felt like asking this as there are always jewels popping up on stackoverflow :) 感觉就像问这个,因为总是有一些珠宝突然出现在stackoverflow :)
What I have is the following list: 我所拥有的是以下列表:
list1 = [['command','arg1','arg2'], ['command2','arg1'], ... ]
How would you recommend to transform it into a string in order to be passed as ONE GET argument? 您如何建议将其转换为字符串以便作为ONE GET参数传递?
eg 例如
http://webgame_site.com/command_list/?data=...
What I am currently doing is separating lists using commas ,
;
什么我目前做的是分离使用逗号名单
,
;
, but I don't like this idea as the method would break if I decide to introduce these within strings. ,但我不喜欢这个想法,因为如果我决定在字符串中引入这些方法会破坏方法。
I'm trying to be as compact as possible. 我想尽可能地紧凑。
One idea I've had is to encode the list into base64: 我有一个想法是将列表编码为base64:
[['command','arg1','arg2'], ['command2','arg1']]
=> "W1snY29tbWFuZCcsJ2FyZzEnLCdhcmcyJ10sWydjb21tYW5kMicsJ2FyZzEnXV0="
which is shorter than URIencode 它比URIencode短
Any ideas? 有任何想法吗? :)
:)
Convert it to json then encode the characters using encodeURI. 将其转换为json,然后使用encodeURI对字符进行编码。
var list1 = [['command','arg1','arg2'], ['command2','arg1']];
var encoded = encodeURI(JSON.stringify(list1));
alert(encoded);
Edit for base64: 编辑base64:
var list1 = [['command','arg1','arg2'], ['command2','arg1']];
var encoded = btoa(JSON.stringify(list1));
alert(encoded);
alert(atob(encoded));
jQuery.param()听起来不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.