How spring MVC receives arrays of JSON objects that are passed in from the foreground

Source: Internet
Author: User

How spring MVC receives arrays of JSON objects that are passed in from the foreground

Main methods:

(The main package used is Net.sf.json, i.e.: Json-lib-2.3-jdk15.jar

Full-related JAR package:

Commons-beanutils-1.7.0.jar
Commons-collections-3.2.jar

Commons-lang-2.4.jar

Commons-logging-1.1.jar

Ezmorph-1.0.4.jar

Json-lib-2.2.2-jdk15.jar)

Convert the Jsonarray JSON array to the list of entity classes:

1 // T is a casually defined generic, you define the type according to your own needs 2 // Dolist is a JSON type string 3  Public List<t> dolist (String dolist) {  4 jsonarray jsonarray=jsonarray.fromobject (dolist); 5 list<t> list= (List) jsonarray.tocollection (jsonarray,t.class); 6 return list; 7 }   

Iterate over the JSON array:

1 iterator<object> it = jsonarray.iterator (); 2  while (It.hasnext ()) {3 jsonobject ob = (jsonobject) It.next (); 4 }

------

------

Specific code:

JS Code:

1 functionsubmituserlist () {2Alert ("OK");3             varCustomerarray =NewArray ();4Customerarray.push ({id: "1", Name: "John Doe", PWD: "123"});5Customerarray.push ({ID: "2", Name: "Zhang San", pwd: "332"});6             7 $.ajax ({8URL: "/user/submituserlist_3",9Type: "POST",TenDataType: "JSON", One data: { A                 //"Dolist": json.stringify (Customerarray),//Serializing JSON object to JSON string, json.stringify () primitive Ecological Method -"Dolist": $.tojson (Customerarray) -},//serializes a JSON object into a JSON string, ToJSON () requires a reference jquery.json.min.js theSuccessfunction(data) { -                 }, -Errorfunction(res) { - alert (res.responsetext); +                 } -             }); +}

Java code:

Method One:

1 ///If the received parameter can be encapsulated into the JavaBean object, it can be written as follows:2          PublicList < user>dolist (String dolist) {3             //turn the string into Jsonarray first4Jsonarray Jsonarray =Jsonarray.fromobject (dolist);5             //and turn the Jsonarray into a collection of objects .6List < user > list = (list) jsonarray.tocollection (Jsonarray, user.class);7             returnlist;8         }9         

--------

Method Two:

1 ///If the received parameter is not easily encapsulated in a Java object, it can be obtained in the following way2          PublicList < user>dolist (String dolist) {3             //turn the string into Jsonarray first4Jsonarray Jsonarray =Jsonarray.fromobject (dolist);5             //iterative Jsonarray6Iterator<object> it =jsonarray.iterator ();7List<user> list=NewArraylist<user>();8              while(It.hasnext ()) {9Jsonobject ob = (jsonobject) it.next ();//take to each Jsonobject objectTenUser User =NULL; One                 if(Ob.getstring ("id")! =NULL){//Call Jsonobject's GetString ("property") method to get the corresponding value AUser=NewUser (); -User.setid (ob.getstring ("id")); -                 } the                 if(Ob.getstring ("name")! =NULL){ -User.setname (ob.getstring ("name")); -                 } -                 if(user!=NULL){ + list.add (user); -                 } +             } A             returnlist; at}

How spring MVC receives arrays of JSON objects that are passed in from the foreground

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.