[1] Introduction to JSON
> JSON full name JavaScript Object Notation
> methods similar to the creation of objects in JS
> JSON, like XML, is a format for representing data
> But JSON is much higher than XML's storage and parsing performance, and JSON is about 30% taller than XML.
<user>
<name>sunwukong</name>
<age>18</age>
<gender> Men </gender>
</user>
{"Name": "Monkey King", "Age": 8, "Gender": male}
[2] format of JSON
> JSON string is not easy to read, but good transmission performance
> XML for easy reading, but poor transmission performance
> JSON format and JS object type, but requires that the property name must use double quotation marks. You can't use single quotes or write quotes!
> JSON object is actually a set of key-value pairs of the structure,
Keys and values use: connection, multiple key-value pairs used, separate, note if it is the last set of key-value pairs, then do not add,.
For example: {"Property name 1": Property value 1, "Property Name 2": Property value 2, "Property name 3": Property value 3, "Property name 4": Property value 4}
> JSON Run property value type:
1. String
2. Digital
3. Boolean
4. Objects
5. Arrays
6.null
> Arrays:
[Attribute 1, Property 2, Property 3, property 4]
[3] using JSON in JS
JSON object---JSON string
Json.stringify (object)
JSON string---JSON object
Json.parse (JSON string)
[4] using JSON in Java
> The more JSON parsing tools available in Java today:
Json-lib--use trouble, worst parsing performance
Jackson--use more hassle, analytic performance best
Gson--Easy to use, analytical performance can
-Gson is a JSON parsing tool from Google that is simple to use and performs better.
Java Object---JSON string
Convert map to JSON string map<string,string> map=new HashMap () map.put ("name", "Zhang San"), Map.put ("hobby", "basketball"), Map.put (" Genter "," male "); String Str=gson.tojson (map);
JSON strings---Java objects
Map<string,string> Map2=gson.fromjson (str, map.class); System.out.println (MAP2);
[3]. Ajax implementation via jquery
> There are caching problems with Get and getjson, and you cannot transfer more data by using the Get method. The
> Post method does not have a caching problem, so we use more post methods when developing.
[1] Post () method
$.post (URL, [data], [callback], [ Type])
parameters:
URL: Send Ajax request Address, String.
data: Request parameters sent to the server, in JSON format.
callback: We can pass this callback function when we currently need to get the response sent by the server.
jquery Returns the response information as a parameter to the callback function
Type: the kind of response information, a string. General two common values text, JSON
[2] Get () method
- The Get method and the Post method are used basically the same way.
[3] Getjson () method
getjson (URL, [data], [ Callback])
The Getjson method is similar to the Get method, except that the default response type of the method is JSON, which does not need to be specified manually.
[4] The following gives a small example: (This example page is very simple, just to achieve this one function)
First import the jar packages required by jquery:
I put this jar package into the JS folder under WebContent (JS folder is built by itself).
Then we introduce jquery into the JSP.
<script type= "Text/javascript" src= "${pagecontext.request.contextpath}/js/jquery-3.2.1.js" ></script>
Then there is the JSP:
<%@ 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" >
This allows the backend to receive this jquery and JSON-based GET request.
Introduction to JSON data presentation format (JavaScript Object notation)