1. Issue asynchronous requests for get and post of jQuery?
get:
Syntax: $. get(1. "Request path", 2. Anonymous function for processing response data, in which the formal parameter is the value returned by the server to the client, 3. Type of response data (text by default))
post:
$. post("1. Request path", "2. Request parameters", 3. Anonymous function for processing response data, corresponding data type (text by default))
ajax request for jquery
Attribute name | explain |
---|---|
url | Requested server-side url address |
async | (default: true) by default, all requests are asynchronous. If you need to send a synchronization request, set this option to false |
data | The data sent to the server can be in the form of key value pairs or js objects |
type | (default: "GET") request method ("POST" or "GET"), the default is "GET" |
dataType | The expected type of returned data. The values can be XML, HTML, script, JSON, text_ Defaul et al |
success | Callback function after successful request |
error | This function is called when the request fails |
//Declare a method to send asynchronous requests using jQuery's ajax method function fn5() { $.ajax({ url:"/demo01", //Request path data:"username=lucy", //Request parameters type:"POST", //Request mode success:function (result) { $("#msg").html(result) }, //Callback function when the request is successful error:function () { $("#msg").html(" server exception ") } //Callback function when the request fails }) }
2. Performance of JSON data format
Front end: {key:value},
3. Why use Json and how to use Json data format?
- During the use of Ajax, the data returned by the server may be complex, such as list < user >; these data are usually converted into json format and the json format string is returned to the client
- Common conversion tools are:
- Jackson: Spring MVC's built-in conversion tool
- jsonlib: conversion tool provided by Java (generally not used)
- gson: conversion tool provided by google (Lightweight Framework)
- fastjson: conversion tool provided by Alibaba (high efficiency and fast speed)
fastjson link: https://pan.baidu.com/s/17D1_OjSRDbge01Vb_ZoUJQ
Extraction code: aio7
jackson link: https://pan.baidu.com/s/1L6izFuiaDFjyo3XbMHyolw
Extraction code: 2ytf
jackson's core method of converting Java objects into json string format
- Jackson provides the core class of transformation: ObjectMapper
- Construction method of ObjectMapper: parameterless construction
- Common methods of ObjectMapper:
method | explain |
---|---|
writeValueAsString(Object obj) | Convert the data in the obj object into json format |
fastjson is the core method to convert Java objects into json string format
- fastjson provides the core class: JSON
- JSON provides some common static methods:
method | explain |
---|---|
toJSONString(Object obj) | Convert the data in the obj object into json format |
Search case
4. Convert JSON object into java object?
------------>Convert Jackson string to java object
method | function |
---|---|
readValue(String json, Class type) | Restore the json string to a Java object of type |
readValue(String json, TypeReference reference) | Restore the json string to a complex Java object with generics (such as List) |
1. Call JSON.parseObject(String json,Class clazz)
String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"}"; //1. Call JSON.parseObject(String json,Class clazz); //Convert to user User user = JSON.parseObject(jsonStr, User.class); System.out.println(user);
2. Use jackson to convert json array string into List
String jsonStr = "[{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"},{\"id\":2,\"username\":\"ls\",\"password\":\"123456\",\"email\":\"ls@163.com\",\"phone\":\"1386781898\"},{\"id\":3,\"username\":\"ww\",\"password\":\"123456\",\"email\":\"ww@163.com\",\"phone\":\"1386782898\"}]"; //1. Create ObjectMapper object ObjectMapper objectMapper = new ObjectMapper(); //2. Call readValue() TypeReference<List<User>> ref = new TypeReference<List<User>>() { }; List<User> list = objectMapper.readValue(jsonStr, ref); System.out.println(list);
------------>Convert fastjson string to java object
method | function |
---|---|
parseObject(String json, Class type) | Restore the json string to a Java object of type |
parseArray(String json, Class type) | Restore the json string to List |
1. Use fastjson to convert json string into JavaBean object or Map
//Convert json to JavaBean(user object) public void test08() throws IOException { String jsonStr = "{\"id\":1,\"username\":\"zs\",\"password\":\"123456\",\"email\":\"zs@163.com\",\"phone\":\"1386789898\"}"; //1. Call JSON.parseObject(String json,Class clazz); User user = JSON.parseObject(jsonStr, User.class); System.out.println(user);
2. / / use fastjson to convert the string of json array into List
String jsonArr = "[{\"id\":1,\"username\":\"Obama\",\"password\":\"123456\",\"email\":\"123456@qq.com\",\"phone\":\"18999999999\"},{\"id\":2,\"username\":\"Jay Chou stick\",\"password\":\"654321\",\"email\":\"654321@qq.com\",\"phone\":\"18666666666\"},{\"id\":3,\"username\":\"Wang Lihong\",\"password\":\"777777\",\"email\":\"777777@qq.com\",\"phone\":\"18777777777\"}]"; List<User> userList = JSON.parseArray(jsonArr, User.class); for (User user : userList) { System.out.println(user.getUsername()); } }