首先,定义一个实体类Person:
import com.fasterxml.jackson.annotation.JsonFormat; import java.util.Date; public class Person { private String name; private int age ; private String gender; //@JsonIgnore // 忽略该属性 @JsonFormat(pattern = "yyyy-MM-dd") private Date birthday;//该属性用来讲解注解的使用 public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
3. JSON数据和Java对象的相互转换
* JSON解析器:
* 常见的解析器:Jsonlib,Gson,fastjson,jackson
1. JSON转为Java对象
1. 导入jackson的相关jar包
2. 创建Jackson核心对象 ObjectMapper
3. 调用ObjectMapper的相关方法进行转换
* readValue(json字符串数据,Class)
//演示 JSON字符串转为Java对象 @Test public void test5() throws Exception { //1.初始化JSON字符串 String json = "{\"gender\":\"男\",\"name\":\"张三\",\"age\":23}"; //2.创建ObjectMapper对象 ObjectMapper mapper = new ObjectMapper(); //3.转换为Java对象 Person对象 Person person = mapper.readValue(json, Person.class); System.out.println(person); }
2. Java对象转换JSON
1. 使用步骤:
1. 导入jackson的相关jar包
2. 创建Jackson核心对象 ObjectMapper
3. 调用ObjectMapper的相关方法进行转换
1. 转换方法:
* writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
* writeValueAsString(obj):将对象转为json字符串
import cn.itcast.domain.Person; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; import java.io.FileWriter; import java.util.*; public class JacksonTest { //Java对象转为JSON字符串 @Test public void test1() throws Exception { //1.创建Person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setGender("男"); //2.创建Jackson的核心对象 ObjectMapper ObjectMapper mapper = new ObjectMapper(); //3.转换 /* 转换方法: writeValue(参数1,obj): 参数1: File:将obj对象转换为JSON字符串,并保存到指定的文件中 Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中 OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中 writeValueAsString(obj):将对象转为json字符串 */ String json = mapper.writeValueAsString(p);//System.out.println(json);//输出结果{"name":"张三","age":23,"gender":"男"} //writeValue,自动创建一个a.txt文件,将数据写到d://a.txt文件中 //mapper.writeValue(new File("d://a.txt"),p); //writeValue,将数据关联到Writer中,同上面一样,自动创建一个b.txt文件,将数据写到d://b.txt文件中 mapper.writeValue(new FileWriter("d://b.txt"),p); }}
2. 注解:
1. @JsonIgnore:排除属性。
@JsonIgnore // 忽略该属性 private Date birthday;
这样注解后,打印的json字符串中将不会有该属性键值对。
2. @JsonFormat:属性值的格式化
* @JsonFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd") private Date birthday;
该注解可以定义属性值的格式,json字符串中该属性值将yyyy-MM-dd格式展示。
@Test public void test2() throws Exception { //1.创建Person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setGender("男"); p.setBirthday(new Date()); //2.转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(p); System.out.println(json); //birthday格式化前:{"name":"张三","age":23,"gender":"男","birthday":1530958029263} //birthday格式化后:{"name":"张三","age":23,"gender":"男","birthday":"2018-07-07"} }
3. 复杂java对象转换
1. List:转换后与数组格式 [ ...] 一致
@Test public void test3() throws Exception { //1.创建Person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setGender("男"); p.setBirthday(new Date()); Person p1 = new Person(); p1.setName("张三"); p1.setAge(23); p1.setGender("男"); p1.setBirthday(new Date()); Person p2 = new Person(); p2.setName("张三"); p2.setAge(23); p2.setGender("男"); p2.setBirthday(new Date()); //创建List集合 List<Person> ps = new ArrayList<Person>(); ps.add(p); ps.add(p1); ps.add(p2); //2.转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(ps); // [{},{},{}] //打印结果[{"name":"张三","age":23,"gender":"男","birthday":"2018-07-07"},{"name":"张三","age":23,"gender":"男","birthday":"2018-07-07"},{"name":"张三","age":23,"gender":"男","birthday":"2018-07-07"}] System.out.println(json); }
2. Map:转换后与对象格式 {...} 一致
@Test public void test4() throws Exception { //1.创建map对象 Map<String,Object> map = new HashMap<String,Object>(); map.put("name","张三"); map.put("age",23); map.put("gender","男"); //2.转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(map); System.out.println(json);//打印结果{"gender":"男","name":"张三","age":23} }