最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Good way to serialize a list? - JavascriptAJAX - Stack Overflow

programmeradmin1浏览0评论

just felt like asking this as there are always jewels popping up on stackoverflow :)

What I have is the following list:

list1 = [['mand','arg1','arg2'], ['mand2','arg1'], ... ]

How would you remend to transform it into a string in order to be passed as ONE GET argument?

e.g.

/?data=...

What I am currently doing is separating lists using mas , ; , 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 pact as possible.


One idea I've had is to encode the list into base64:

[['mand','arg1','arg2'], ['mand2','arg1']]
=> "W1snY29tbWFuZCcsJ2FyZzEnLCdhcmcyJ10sWydjb21tYW5kMicsJ2FyZzEnXV0="

which is shorter than URIencode


Any ideas? :)

just felt like asking this as there are always jewels popping up on stackoverflow :)

What I have is the following list:

list1 = [['mand','arg1','arg2'], ['mand2','arg1'], ... ]

How would you remend to transform it into a string in order to be passed as ONE GET argument?

e.g.

http://webgame_site./mand_list/?data=...

What I am currently doing is separating lists using mas , ; , 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 pact as possible.


One idea I've had is to encode the list into base64:

[['mand','arg1','arg2'], ['mand2','arg1']]
=> "W1snY29tbWFuZCcsJ2FyZzEnLCdhcmcyJ10sWydjb21tYW5kMicsJ2FyZzEnXV0="

which is shorter than URIencode


Any ideas? :)

Share Improve this question edited Dec 22, 2010 at 3:32 RadiantHex asked Dec 22, 2010 at 3:15 RadiantHexRadiantHex 25.6k47 gold badges155 silver badges251 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Convert it to json then encode the characters using encodeURI.

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

alert(encoded);

Edit for base64:

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

alert(encoded);
alert(atob(encoded));

jQuery.param() sounds good.

发布评论

评论列表(0)

  1. 暂无评论