SPRINGMVC related-return JSON data

Source: Internet
Author: User

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

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.