Springmvc How to process JSON data?
The steps are as follows:
1. Add 3 jar packages to JSON
Jackson-annotations-2.1.5.jar
Jackson-core-2.1.5.jar
Jackson-databind-2.1.5.jar
2. Write the target method so that it returns the JSON corresponding object or collection
3. Add @ResponseBody Annotations on the method:
Here the method borrows from jquery (AJAX): 1, import jar package: Jquery-1.9.1.min.js 2, import package on JSP page: <script type= "Text/javascript" src= "${pagecontext . Request.contextpath}/js/jquery-1.9.1.min.js "></script> 3. Code that calls JSON: <script type=" Text/javascript "& Gt $ (function () {/* alert ("Hello JQuery"); */$ ("#btn"). Click (function () {var url= "${pagecontext.req Uest.contextpath}/testjson "; var data={}; function callback (DATA1) {for (Var i=0;i<data1.length;i++) {alert (data1[i].id+ "= = =" +d Ata1[i].name); }} $.post (Url,data,callback); }); }); </script> 4, the corresponding jquery call button: <button id= "btn" > button </button>json Method use: Create class://Use annotations @res Ponsebody, the resulting data is automatically converted to json@requestmapping (value= "Testjson", Method=requestmethod.post) @ResponseBody public list< User> getList () {list<user> user=new arraylist<user> (); User.add (New USer ("Zhang San", 1)); User.add (New User ("John Doe", 2)); User.add (New User ("Harry", 1)); return user; The user encapsulated object used in the above class: public class User {private String name; Private Integer ID; }
SPRINGMVC related-return JSON data