將json轉換成struts參數

加入對象爲{name:'tom','class':{className:'class1'},classMates:[{name:'lily'}]}
struts2期待的格式是 name=tom&class.className=class1&classMates[0].name=lilythis

function parseParam(param, key) {
    var paramStr = "";
    if (param instanceof String || param instanceof Number || param instanceof Boolean
    ) {
        paramStr += "&" + key + "=" + encodeURIComponent(param);
    }
    else {
        $.each(param, function (i, p) {
            if (p == null || p == undefined)
                return true;
            var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
            paramStr += '&' + parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};

 

//調用:
var obj={name:'tom','class':{className:'class1'},classMates:[{name:'lily'}]};
parseParam(obj);
//結果:
"name=tom&class.className=class1&classMates[0].name=lily"
parseParam(obj,'stu');
//結果:
"stu.name=tom&stu.class.className=class1&stu.classMates[0].name=lily"spa

 

 

若是這篇文章對您有幫助,您能夠打賞我code

 

技術交流QQ羣:15129679對象

相關文章
相關標籤/搜索