简体   繁体   English

序列化列表的好方法? - Javascript / AJAX

[英]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.

相关问题 为ajax请求序列化javascript对象的递归函数-有没有更简单的方法? - Recursive function to serialize a javascript object for an ajax request - is there an easier way? JavaScript中良好作用域的方式 - The way of good scoping in JavaScript 在Codeigniter中纠正Ajax的好方法? - Good way to right ajax in Codeigniter? ASP.NET MVC通过Java序列化到Controller的AJAX通过BeginCollectionItem List的对象传递null - ASP.NET MVC Passing a BeginCollectionItem List's Object through AJAX with Javascript Serialize to Controller is passing null 用ajax更新服务器从一个列表中选择对象并将它们移动到另一个列表中的好方法是什么? - What is the good way to select objects from one list and move them into another list with ajax updating the server? 将javascript交互附加到通过ajax加载的网页部分的好方法是什么? - What is a good way to attach javascript interactions to the part of webpage which is loaded via ajax? 用 Javascript 扩展原型 - 好方法? - Extending prototypes in Javascript - good way? 传递信息javascript的好方法? - A good way to pass information javascript? 有没有一种方法可以反序列化javascript中的php序列化字符串? - Is there a way to unserialize a php serialize string in javascript? 在 JavaScript 中序列化/反序列化对象的最佳方法? - Best way to serialize/unserialize objects in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM