关于JSON
1 JSON概述
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。
它基于 ECMAScript (W3C制定的JS规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。
简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。
易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
2 JSON语法
- [] 表示json数组
- {} 表示json对象
- “” 表示是属性名或字符串类型的值
- : json数据都是由key和value组成
- , json中的数据与数据之间的分隔
- 举例:
- JSON对象
- {name:“宋柠檬”,age:30,gender :“女”,birthday:“2023-03-28”}
- JSON数组
- [{name:“宋柠檬”,age:30,gender:“女”,birthday:“2023-03-28”}, {name:“李柠檬”,age:25,sex:“男”,birthday:“2023-03-27”}]
3 Json技术
Json技术: JSON字符串的生成和解析
- FastJson 阿里里巴巴
- JackSon github
- Gson google公司
4 JackSon
- 创建ObjectMapper对象
- ObjectMapper mapper = new ObjectMapper();
- 将json字符串生成Java对象
- mapper.readValue(json字符串,类对象)
- 将json数组字符串生成Java对象
- mapper.readValue(json字符串,数组[].class);
- 先将字符串生成Java数组,再将数组转换成集合
- mapper.readValue(json字符串,new TypeReference<ArrayList>(){});
- mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, User.class));
- 将Java对象解析成json字符串
- mapper.writeValueAsString(对象|集合);
注意:
- 日期类型转换的时候需要加上注解
- @JsonFormat(pattern= “yyyy-MM-dd HH:mm:ss”,timezone = “GMT+8”)
- 如果字段名与字符串中的key不一致。
- @JsonProperty(“address”)
- 忽略实体类中的属性(不进行json转换)
- @JsonIgnore
public class TestJackson {
//将java对象转换成json字符串
@Test
public void test01() throws JsonProcessingException {
Emp emp = new Emp(4399,"前端","开发",7777,new Date(),4000d,0d,20,null);
//将java对象转换成json字符串
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(emp);
System.out.println(json);
}
//将java对象集合转换成json字符串
@Test
public void test02() throws JsonProcessingException {
List<Emp> empList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Emp emp = new Emp(4399+i,"前端"+i,"开发"+i,7777,new Date(),4000d,0d,20,null);
empList.add(emp);
}
//将java对象集合转换成json字符串
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(empList);
System.out.println(json);
}
//将json格式的字符串转成java对象
@Test
public void test03() throws IOException {
String json = "{\"empno\":4399,\"ename\":\"前端\",\"job1\":\"开发\",\"mgr\":7777,\"hiredate\":1634007374014,\"sal\":4000.0,\"comm\":0.0,\"deptno\":20,\"image\":null}";
//将json格式的字符串转成java对象
ObjectMapper mapper = new ObjectMapper();
Emp emp = mapper.readValue(json, Emp.class);
System.out.println(emp);
}
//将json数组格式的字符串转成java对象集合
@Test
public void test04() throws IOException {
String json = "[{\"empno\":4399,\"ename\":\"前端0\",\"job\":\"开发0\",\"mgr\":7777,\"hiredate\":1634007490425,\"sal\":4000.0,\"comm\":0.0,\"deptno\":20,\"image\":null},{\"empno\":4400,\"ename\":\"前端1\",\"job\":\"开发1\",\"mgr\":7777,\"hiredate\":1634007490425,\"sal\":4000.0,\"comm\":0.0,\"deptno\":20,\"image\":null},{\"empno\":4401,\"ename\":\"前端2\",\"job\":\"开发2\",\"mgr\":7777,\"hiredate\":1634007490425,\"sal\":4000.0,\"comm\":0.0,\"deptno\":20,\"image\":null},{\"empno\":4402,\"ename\":\"前端3\",\"job\":\"开发3\",\"mgr\":7777,\"hiredate\":1634007490425,\"sal\":4000.0,\"comm\":0.0,\"deptno\":20,\"image\":null},{\"empno\":4403,\"ename\":\"前端4\",\"job\":\"开发4\",\"mgr\":7777,\"hiredate\":1634007490425,\"sal\":4000.0,\"comm\":0.0,\"deptno\":20,\"image\":null}]";
//将json数组格式的字符串转成java对象集合
ObjectMapper mapper = new ObjectMapper();
//方式1:
//Emp[] emps = mapper.readValue(json, Emp[].class);
//将数组转换成List集合
//List<Emp> empList = Arrays.asList(emps);
//empList.forEach(System.out::println);
//方式2:
List<Emp> empList = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Emp.class));
empList.forEach(System.out::println);
}
}