1. 程式人生 > 其它 >JavaWeb22.3【Ajax&Json:JSON資料和Java物件的相互轉換】

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 }
a.txt = b.txt
{"name":"張三","age":23,"gender":"男"}