SPRINGMVC binding JSON parameter bis (2.2.3)

Source: Internet
Author: User

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)

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.