JavaWeb-Ajax-Axios
JavaWeb-Ajax-Axios
JSON-概述
什么是JSON
?
JSON(JavaScript Object Notation)
,JS
对象标记, 是一种轻量级别的数据交换格式. 它基于 ECMAScript(w3c)
制定的 js
规范的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON
成为理想的数据交换语言, 易于人阅读和编写, 同时也易于机器解析和生成。并有效地提升网络传输效率。
FastJSON
-
FastJson
是一个 Java
库, 可以将 Java
对象转换为 JSON
格式, 当然它也可以将 JSON
字符串转换为 Java
对象 -
提供了
toJSONString()
和 parseObject()
方法来将 Java
对象与 JSON
相互转换 - 调用
toJSONString()
方法即可将对象转换成 JSON
字符串 parseObject
方法则反过来将 JSON
字符串转换成对象
- 调用
-
使用
toJSONString
-
添加
maven
依赖 1
2
3
4
5
6<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency> -
使用测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21实体类:
public class Users {
private int uid; // 用户实体的主键属性
private String uname; // 用户账号
private String upassword; // 用户密码
private String uemail; // 用户邮箱 用于激活使用
private String usex; // 用户性别
private int ustatus; // 用户激活状态 0 未激活 1 激活
private String ucode; // 邮件激活码
private int urole; // 用户 0 管理员 1
// JavaBean...
}单元测试:
public void testFastJson() {
Users users = new Users(2022, "coderitl", "coderitl", "123xxx@qq.com", "男", 1, "2022", 2);
// 将实体类转换为 json 格式
String strToJsonUser = JSON.toJSONString(users);
System.out.println(strToJsonUser);
} -
输出
JSON 格式在线校验
-
测试
1
2
3
4
5
6
public void testFastJsonStrToEntity() {
String message = "{\"ucode\":\"2022\",\"uemail\":\"123xxx@qq.com\",\"uid\":2022,\"uname\":\"coderitl\",\"upassword\":\"coderitl\",\"urole\":2,\"usex\":\"男\",\"ustatus\":1}\n";
Users users = JSON.parseObject(message, Users.class);
System.out.println(users);
} -
输出
1
Users{uid=2022, uname='coderitl', upassword='coderitl', uemail='123xxx@qq.com', usex='男', ustatus=1, ucode='2022', urole=2}
-
Jackson 解析
-
Jackson
是一个能否将 Java
对象序列化为 JSON
字符串, 也能够将 JSON
字符串反序列化为 Java
对象的框架 -
通过方法
readvalue
和 writeValue
实现 -
使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>-
测试
1
2
3
4
5
6
7
public void testJackSon() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Users users = new Users(2022, "coderitl", "coderitl", "123xxx@qq.com", "男", 1, "2022", 2);
String s = mapper.writeValueAsString("testJackSon: "+users);
System.out.println(s);
} -
输出
JackSON 检验
-
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void testJackSon() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
// JSON 字符串
String message = "{\n" +
"\t\"uid\": 2022,\n" +
"\t\"uname\": \"coderitl\",\n" +
"\t\"upassword\": \"coderitl\",\n" +
"\t\"uemail\": \"123xxx@qq.com\",\n" +
"\t\"usex\": \"男\",\n" +
"\t\"ustatus\": 1,\n" +
"\t\"ucode\": \"2022\",\n" +
"\t\"urole\": 2\n" +
"}";
Users users = mapper.readValue(message, Users.class);
System.out.println(users);
} -
输出
-
浏览器处理JSON
-
测试
1
2
3
4
5
6
7
8
9// 对象
const obj = {name: "coder-itl", age: 20, address: "testJson"};
console.log(typeof (obj)); // object
// 将浏览器对象转换为字符串方法
let res = JSON.stringify(obj);
console.log(typeof (res)); // String
// 将字符串转换为 Object
res = JSON.parse(res);
console.log(typeof (res)); // object -
浏览器处理
json
字符串 JSON.stringigy()
-
浏览器转换为
JSON
对象 JSON.parse
什么 Ajax
Ajax
是一种在无需重新加载整个网页的情况下, 能够更新部分网页的技术 Ajax==Asyncchronous
异步 JavaScript and XML
Ajax
是一种用于创建快速动态网页的技术 - 使用
Ajax
发起异步请求
GET 请求
-
servlet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.coderitl.bookshop.controller;
...
public class data extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String message = "{'name':'coder-itl'}";
// 向外发送数据
PrintWriter writerStream = response.getWriter();
writerStream.write(message);
writerStream.flush();
writerStream.close();
}
}
1 |
<%-- |
POST 请求
1 |
|
1 |
const btn = document.querySelector("#btn"); |
省级联动实战
1 |
-- 创建数据库 |
-
获取
json
数据
-
测试输出