Second, SPRINGMVC receive JSON strings in different formats
4). Format Four: JSON passes complex objects (there are attributes in the object, and list)
Complex objects:
PackageTestvo;Importjava.util.List; Public classTest {PrivateList<user>UU; PrivateString Jsonobjname; PrivateInteger ID; PublicString Getjsonobjname () {returnJsonobjname; } Public voidsetjsonobjname (String jsonobjname) { This. Jsonobjname =Jsonobjname; } PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicList<user>Getuu () {returnUU; } Public voidSetuu (list<user>UU) { This. UU =UU; } }
Where user has the following attribute (with a user[] property to receive, the next section will turn it into list[] for testing):
Private String userName; Private String address; Private User[] users;
The foreground constructs complex object delivery:
Test =function () { //Create a complex JS object varTest = {}; //Add attributes to objects (two ways below), with names consistent with background complex object property namestest["jsonobjname"] = "JsonTest1"; Test.id= 30; //Create a complex Property object List<user>,list is an array in JS varUserList = []; varUser1 = {}; user1["UserName"] = "U1"; user1["Address"] = "ADD1"; //Create the JS object corresponding to the user[] property in user varUserattr = []; varUser2 = {}; user2["UserName"] = "U2"; user2["Address"] = "ADD2"; Userattr.push (User2); //The User object has an array of variables named users, private user[] users; This is where the attribute is added.user1["users"] =userattr; Userlist.push (user1); //complex attributes are added to the JS objecttest["UU"] =userlist; //transfer the constructed JS to the backgroundJquery.ajax ({url:cur_url+ "/weekly/test", type:' Post ', Data:JSON.stringify (test), DataType:' JSON ', ContentType:' Application/json;charset=utf-8 ', Success:function(data, textstatus) {console.info (data); Alert ("Test success!"); }, Error:function() {alert ("Test error!"); } }); };
Background receive:
@RequestMapping ("/test") @ResponseBody PublicList<user>Test (@RequestBody Test TT) { for(User user:tt.getUu ()) {System.out.println ("User:" +user); System.out.println ("UserName:" +user.getusername ()); System.out.println ("Address:" +user.getaddress ()); User[] attr=user.getusers (); for(inti = 0; i < attr.length; i++) {System.out.println ("Attr>>>" +Attr[i]); System.out.println ("Attr_username>>>" +attr[i].getusername ()); System.out.println ("Attr_address>>>" +attr[i].getaddress ()); } } returnTt.getuu (); }
This will bind properly
The next section verifies whether the list and array receive the same effect in a complex object in the background.
SPRINGMVC binding JSON parameter bis (2.2.3)