JS serializes js into a urlencoded method, and Java accepts complex objects.

Source: Internet
Author: User

Server-side

Java definition

class Role (var name:string = "", var remark:string = ""class User (var name:string = "", var roles:array<role> =class Corp (var name:string = "", var users:array<user> == "Hi ") Fun Hipost (Qm:corp, session:httpsession): String {}

Java accepted format, Kotlin Test

@Test Fun Testhi () {var map=Jsonmap (); map["Name"] = "Gold Dimension"; map["Users[0].name"] = "Zhang San"; map["Users[0].roles[0].name"] = "Sales"; map["Users[0].roles[0].remark"] = "Sales Role"; map["USERS[0].F"] = "Wanda, Wang er"; map["Users[1].name"] = "Ben Four"; map["Users[1].roles[0].name"] = "development"; map["Users[1].roles[0].remark"] = "Development Role"; map["Users[1].roles[1].name"] = "UI"; map["Users[1].roles[1].remark"] = "designer";//map["createat"] = 1492762310797;var result= mvc!!. Perform (Mockmvcrequestbuilders.post ("/hi"). ContentType (Mediatype.application_form_urlencoded_value). Content (URL Encodedutils.format (map.                Tonamevaluepairs (), Charset.defaultcharset ())). Anddo (Mockmvcresulthandlers.print ())        . Andreturn (); Assert.assertequals (Result.response.status,200);    println (result); }

The client needs Postbody content:

name=%e9%87%91%e7%bb%b4%e5%ba%a6&users%5b0%5d.name=%e5%bc%a0%e4%b8%89&users%5b0%5d.roles%5b0%5d.name=% e9%94%80%e5%94%ae&users%5b0%5d.roles%5b0%5d.remark=%e9%94%80%e5%94%ae%e8%a7%92%e8%89%b2&users%5b1% 5d.name=%e6%9c%ac%e5%9b%9b&users%5b1%5d.roles%5b0%5d.name=%e5%bc%80%e5%8f%91&users%5b1%5d.roles%5b0% 5d.remark=%e5%bc%80%e5%8f%91%e8%a7%92%e8%89%b2&users%5b1%5d.roles%5b1%5d.name=ui&users%5b1%5d.roles% 5b1%5d.remark=%e8%ae%be%e8%ae%a1%e5%b8%88

Client

Defined:

/*serialization of the *java mode. * @param obj*/Jv.param_jmap=function(obj) {varRET = {}; Object.keys (obj). ForEach (Key= {    varValue =Obj[key]; if(Array.isarray (value)) { for(varIinchvalue) {        varm =Jv.param_jmap (Value[i]); Object.keys (M). ForEach (SK={Ret[key+ "[" + i + "]." + SK "=M[sk]; })      }    }    Else if(Jv.isplainobject (value)) {varm =Jv.param_jmap (value); Object.keys (M). ForEach (SK={Ret[key+ "." + SK] =M[sk]; })    }    Else{Ret[key]=value; }  })  returnret;} Jv.param=function(obj) {varRET =jv.param_jmap (obj);  Console.log (ret); returnObject.keys (ret). Map (it {    returnencodeURIComponent (IT) + "=" +encodeURIComponent (Ret[it])}). Join ("&");}

Test:

var data =    {name:"Gold Dimension", Users:      [        {name:"Zhang San", Roles:[{name: "Sales", remark: "Sales role "}]},        {name:" Ben IV ", Roles:[{name:" Development ", remark:" Development Role "},{name:" UI ", Remark:" Designer "}]}      ]    }  var url = jv.param (data);

JS serializes js into a urlencoded method, and Java accepts complex objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.