As a lightweight data interchange format, JSON occupies a very important position in the former background data exchange. The syntax for JSON is very simple, using a key-value pair representation. JSON converts a set of data represented in a JavaScript object to a string, which can then be easily passed between functions, or the string is passed from a WEB client to a server-side program in an asynchronous application. You can also pass a JSON-formatted string from a server-side program to the front end and be interpreted by the front end. This string is in JSON syntax, and the JSON syntax is a subset of JavaScript syntax, so JavaScript is easy to interpret, and JSON can represent a more complex structure than name/value pairs. Let's take a look at how jquery delivers/parses JSON-formatted data in the following examples.
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://www.w3.org/TR/html4/loose.dtd" >
2. Resolve front-end data using JavaBean:
Package com.tgb.web.controller.annotation;
Import java.io.IOException;
Import Java.net.URLDecoder;
Import java.util.ArrayList;
Import java.util.List;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Com.tgb.web.controller.entity.User; @Controller @RequestMapping ("/user/data") public class Datacontroller {//Receive a JSON object in a string format sent from the foreground, parsed in the background @RequestMap Ping ("/resolvejsonobject") public void Resolvejsonobject (httpservletrequest request,httpservletresponse response) th
Rows IOException {//decoding String str = urldecoder.decode (Request.getparameter ("Orderjson"), "UTF-8");
Jsonobject jb=new jsonobject ();
Converts a string in JSON format to a JSON object and obtains the object's "UserName" property value string o= (String) jb.fromobject (str). Get ("UserName");
System.out.println (o);///Pass the JSON array string @RequestMapping ("/resolvejsonarray") public void Resolvejsonarray (HttpServletRequest request,ht Tpservletresponse response) throws IOException {//decode, in order to solve the Chinese garbled String str = urldecoder.decode (request.getparamet
ER ("Orderjson"), "UTF-8");
Jsonobject jb=new jsonobject ();
Converts a string in JSON format to a JSON array object Jsonarray array= (jsonarray) jb.fromobject (str). Get ("menu"); Gets the first object in the JSON array jsonobject o = (jsonobject) array.get (0);//Get the first array result//Fetch the "UserName" property value of the first object in the JSON array St
Ring Name=o.get ("UserName"). toString ();//Get property value System.out.println (name); //Returns JSON-formatted data in the foreground through jquery parsing @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 ();
User.setusername ("name_" + i);
M.add (user);
}for (int j=0;j<m.size (); j + +) {Jsonobject jsonobject = new Jsonobject ();
Jsonobject.put ("User", M.get (j));
Jsons.add (Jsonobject);
} response.getwriter (). Print (jsons.tostring ());
@RequestMapping ("/tojson") public String Tojson () {return "/json"; }
}
The function of JSON is not only to pass the string in front of the background, but when we use JSON to pass the data, it is more important to consider its transmission efficiency. When the two systems need to exchange data, if you pass a serialized object, the efficiency is very low, and if you are passing an array of large objects, the efficiency is more than you can imagine, and if you pass the object or data to a JSON string, the efficiency is much higher. In this paper, the data transmission and parsing of the front and back of a single system are explained, and the JSON transmission between heterogeneous systems is not covered in this paper.
The above is the SPRINGMVC framework of jquery to pass and parse JSON format data is how to achieve the relevant information, I hope you like.