As a lightweight data exchange format, json occupies a very important position in front and back-end data exchange. Json's syntax is very simple, using key-value pair representation. JSON can convert a set of data represented in a JavaScript object into a string, and then pass this string between functions easily, or pass a string from a web client to a server-side program in an asynchronous application, or pass a string in json format from a server-side program to the front-end and interpreted by the front-end. This string is in line with json syntax, which is a subset of javascript syntax, so javascript is easy to interpret, and JSON can represent a more complex structure than "name/value pair". Let’s take an example to see how JQuery passes/parsesses json format data is implemented.
1. First look at the front-end jsp code:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/"> <html> <head> <script type="text/javascript" src="/springMVC6/js/jquery-1.7."></script> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> $(document).ready(function(){ // Pass a string format json object to the background (a json object) $("#resolveJsonObject").click(function(){ var userName =encodeURI($("#userName").attr("value")); var age = encodeURI($("#age").attr("value")); var user = {userName:userName,age:age}; var aMenu = encodeURI((user)); $.ajax({ url:"/springMVC6/user/data/resolveJsonObject" , data:"orderJson=" + aMenu, success:function(data){ } }); }); //Pass json array to background $("#resolveJsonArray").click(function(){ var userName =encodeURI($("#userName").attr("value")); var age = encodeURI($("#age").attr("value")); //The array starts var user1 = {userName:userName,age:age}; var allMenu={ "menu":[ ] }; (user1); var allMenu1 = encodeURI((allMenu)); $.ajax({ //json array url:"/springMVC6/user/data/resolveJsonArray" , data:"orderJson=" + allMenu1, success:function(data){ } }); }); //Receive json in the background to parse in the foreground $("#resolveJson").click(function(){ $.ajax({ //Parse json data returned from the background url:"/springMVC6/user/data/resolveJson" , type:"post", success:function(data){ var arr=eval(data); alert(); for(var m = 0;m<;m++){ alert(arr[m].); } } }); }); }); </script> </head> <body> <h1>jsonAdd a user</h1> Name:<input type="text" name="userName"> age:<input type="text" name="age"><br> <input type="button" value="json object"> <input type="button" value="json array"> <input type="button" value="Front-end parsing json string"> </body> </html>
2. Use javabean to parse front-end data:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @Controller @RequestMapping("/user/data") public class DataController { //Receive json object in string format transmitted from the front desk and parse it in the background @RequestMapping("/resolveJsonObject" ) public void resolveJsonObject(HttpServletRequest request,HttpServletResponse response) throws IOException { //decoding String str = (("orderJson"),"UTF-8"); JSONObject jb=new JSONObject(); //Convert a string in json format to a json object and get the "userName" attribute value of the object String o=(String)(str).get("userName"); (o); } // Pass json array string @RequestMapping("/resolveJsonArray" ) public void resolveJsonArray(HttpServletRequest request,HttpServletResponse response) throws IOException { //Decode, in order to solve Chinese garbled code String str = (("orderJson"),"UTF-8"); JSONObject jb=new JSONObject(); //Convert json format string to json array object JSONArray array=(JSONArray)(str).get("menu"); //Get the first object in the json array JSONObject o = (JSONObject) (0);//Get the first array result //Take out the "userName" attribute value of the first object in the json array String name=("userName").toString();//Get attribute value (name); } //Return json format data through this function and parse it in the foreground through JQuery @RequestMapping("/resolveJson" ) public void resolveJson(HttpServletRequest request,HttpServletResponse response) throws IOException { List m = (List) new ArrayList(); JSONArray jsons = new JSONArray(); for(int i=0;i<10;i++){ User user = new User(); ("name_" + i); (user); } for(int j=0;j<();j++){ JSONObject jsonObject = new JSONObject(); ("user", (j)); (jsonObject); } ().print(()) ; } @RequestMapping("/toJson" ) public String toJson() { return "/json"; } }
The function of json is not only to pass it in the front and backend as a string. When we use json to pass data, we mainly consider its transmission efficiency. When two systems need to exchange data, if the object is passed, the efficiency is very low. If the array storing a large number of objects is passed, the efficiency is even more unimaginable. At this time, if the object or data is converted into a json string for transmission, the efficiency will be much higher. This article only explains the front and backend data transmission and analysis in a single system, and the json transmission between heterogeneous systems is not within the scope of this article.
The above is related information about how JQuery passes and parses Json format data under the SpringMVC framework. I hope everyone likes it.