Home > Web Front-end > JS Tutorial > body text

Method to convert json into struts parameters

高洛峰
Release: 2016-12-07 13:43:21
Original
1087 people have browsed it

The added object is {name:'tom','class':{className:'class1'},classMates:[{name:'lily'}]}

The format expected by struts2 is name=tom&class.className=class1&classMates[ 0].name=lily

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);
};
Copy after login

//调用:
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"
Copy after login

The above method of converting json into struts parameters is all the content shared by the editor


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template