javascript – 如何将一个对象序列化成参数列表?

在不知道JavaScript对象的键的情况下,我如何转换类似…

var obj = {
   param1: 'something',
   param2: 'somethingelse',
   param3: 'another'
}

obj[param4] = 'yetanother';

… into …

var str = 'param1=something&param2=somethingelse&param3=another&param4=yetanother';

…?

var str = "";
for (var key in obj) {
    if (str != "") {
        str += "&";
    }
    str += key + "=" + encodeURIComponent(obj[key]);
}

示例:http://jsfiddle.net/WFPen/

http://stackoverflow.com/questions/6566456/how-to-serialize-an-object-into-a-list-of-parameters

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:javascript – 如何将一个对象序列化成参数列表?