JavaWeb22.3【Ajax&Json:JSON数据和Java对象的相互转换】

 

 

 

 

复制代码
 1 package com.haifei.domain;
 2 
 3 import com.fasterxml.jackson.annotation.JsonFormat;
 4 import com.fasterxml.jackson.annotation.JsonIgnore;
 5 
 6 import java.util.Date;
 7 
 8 public class Person {
 9 
10     private String name;
11     private int age;
12     private String gender;
13 
14 //    @JsonIgnore
15     @JsonFormat(pattern = "yyyy-MM-dd")
16     private Date birthday;
17 
18     public Person() {
19     }
20 
21     public Person(String name, int age, String gender) {
22         this.name = name;
23         this.age = age;
24         this.gender = gender;
25     }
26 
27     public Person(String name, int age, String gender, Date birthday) {
28         this.name = name;
29         this.age = age;
30         this.gender = gender;
31         this.birthday = birthday;
32     }
33 
34     public Date getBirthday() {
35         return birthday;
36     }
37 
38     public void setBirthday(Date birthday) {
39         this.birthday = birthday;
40     }
41 
42     public String getName() {
43         return name;
44     }
45 
46     public void setName(String name) {
47         this.name = name;
48     }
49 
50     public int getAge() {
51         return age;
52     }
53 
54     public void setAge(int age) {
55         this.age = age;
56     }
57 
58     public String getGender() {
59         return gender;
60     }
61 
62     public void setGender(String gender) {
63         this.gender = gender;
64     }
65 
66     @Override
67     public String toString() {
68         return "Person{" +
69                 "name='" + name + '\'' +
70                 ", age=" + age +
71                 ", gender='" + gender + '\'' +
72                 '}';
73     }
74 }
复制代码
复制代码
  1 package com.haifei.test;
  2 
  3 import com.fasterxml.jackson.core.JsonProcessingException;
  4 import com.fasterxml.jackson.databind.ObjectMapper;
  5 import com.haifei.domain.Person;
  6 import org.junit.Test;
  7 
  8 import java.io.File;
  9 import java.io.FileWriter;
 10 import java.io.IOException;
 11 import java.util.*;
 12 
 13 public class JacksonTest {
 14 
 15     //java对象转为json字符串
 16     @Test
 17     public void test1() throws Exception {
 18         //1 创建java对象
 19         Person p = new Person("张三", 23, "男");
 20 
 21         //2  创建Jackson核心对象 ObjectMapper
 22         ObjectMapper om = new ObjectMapper();
 23 
 24         //3 调用ObjectMapper的相关方法进行转换
 25         /*
 26             转换方法:
 27                 writeValue(参数1,obj):
 28                     参数1:
 29                         File:将obj对象转换为JSON字符串,并保存到指定的文件中
 30                         Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
 31                         OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
 32                 writeValueAsString(obj):将对象转为json字符串
 33         */
 34         String jsonStr = om.writeValueAsString(p);
 35         System.out.println(jsonStr); //{"name":"张三","age":23,"gender":"男"}
 36 
 37         om.writeValue(new File("./a.txt"), p); //day22模块目录下,与src同级
 38         om.writeValue(new FileWriter("./b.txt"), p);
 39     }
 40 
 41 
 42     //特殊:日期类型
 43     @Test
 44     public void test2() throws Exception {
 45         Person p = new Person("张三", 23, "男", new Date());
 46         ObjectMapper mapper = new ObjectMapper();
 47         String json = mapper.writeValueAsString(p);
 48         System.out.println(json);
 49         /*
 50         默认情况下
 51             {"name":"张三","age":23,"gender":"男","birthday":1625740824782}
 52             java对象的日期属性被转为json时,默认为毫秒值
 53 
 54          在javabean中对日期属性进行注解,可以优化
 55              (1)@JsonIgnore:排除属性
 56                 {"name":"张三","age":23,"gender":"男"}
 57              (2)@JsonFormat:属性值的格式化
 58                 {"name":"张三","age":23,"gender":"男","birthday":"2021-07-08"}
 59          */
 60     }
 61 
 62 
 63     //list集合转为json字符串
 64     @Test
 65     public void test3() throws Exception {
 66         Person p1 = new Person("张三", 23, "男", new Date());
 67         Person p2 = new Person("李四", 22, "女", new Date());
 68         Person p3 = new Person("王五", 21, "男", new Date());
 69 
 70         List<Person> ps = new ArrayList<>();
 71         ps.add(p1);
 72         ps.add(p2);
 73         ps.add(p3);
 74 
 75         ObjectMapper mapper = new ObjectMapper();
 76         String json = mapper.writeValueAsString(ps);
 77         System.out.println(json); //[{"name":"张三","age":23,"gender":"男","birthday":"2021-07-08"},{"name":"李四","age":22,"gender":"女","birthday":"2021-07-08"},{"name":"王五","age":21,"gender":"男","birthday":"2021-07-08"}]
 78         //数组格式
 79     }
 80 
 81 
 82     //map集合转为json字符串
 83     @Test
 84     public void test4() throws Exception {
 85         Map<String, Object> map = new HashMap<>();
 86         map.put("name", "张三");
 87         map.put("age", "23");
 88         map.put("gender", "男");
 89 
 90         ObjectMapper mapper = new ObjectMapper();
 91         String json = mapper.writeValueAsString(map);
 92         System.out.println(json); //{"gender":"男","name":"张三","age":"23"}
 93     }
 94 
 95 
 96     //JSON转为Java对象
 97     @Test
 98     public void test5() throws Exception {
 99         //1 初始化json字符串
100         String jsonStr = "{\"gender\":\"男\",\"name\":\"张三\",\"age\":\"23\"}";
101 
102         //2 创建ObjectMapper
103         ObjectMapper mapper = new ObjectMapper();
104 
105         //3 转换为java对象
106         Person person = mapper.readValue(jsonStr, Person.class);
107         System.out.println(person); //Person{name='张三', age=23, gender='男'}
108     }
109 }
复制代码
1
2
a.txt = b.txt
{"name":"张三","age":23,"gender":"男"}

  

posted @   yub4by  阅读(70)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示