一、JSON数据和Java对象的相互转换
* JSON解析器:
* 常见的解析器:Jsonlib,Gson,fastjson,jackson
1. JSON转为Java对象
1. 导入jackson的相关jar包
2. 创建Jackson核心对象 ObjectMapper
3. 调用ObjectMapper的相关方法进行转换
方法: readValue(json字符串数据,Class)
2. Java对象转换JSON
2.1 使用步骤:
1. 导入jackson的相关jar包,可直接下载我的百度网盘中的资源:
链接:https://pan.baidu.com/s/1kacjFVZypdXW8MK0Krrk3g
提取码:h9ak
2. 创建Jackson核心对象 ObjectMapper
3. 调用ObjectMapper的相关方法进行转换
3.1 转换方法:
* writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中
* writeValueAsString(obj):将对象转为json字符串
3.2. 注解:写在java对象类中的成员变量上面
1. @JsonIgnore:排除属性。该属性相关的键值对将不会出现在json对象中
2. @JsonFormat:属性值得格式化。
* @JsonFormat(pattern = "yyyy-MM-dd")
3.3 复杂java对象转换
1. List:数组
2. Map:对象格式一致
二、代码展示:
package domain; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; 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 + ‘\‘‘ + ", birthday=" + birthday + ‘}‘; } }
package test; import com.fasterxml.jackson.databind.ObjectMapper; import domain.Person; import org.junit.Test; 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.创建核心对象objectmapper ObjectMapper mapper=new ObjectMapper(); //3.调用方法 /* 3.1. 转换方法: * writeValue(参数1,obj): 参数1: File:将obj对象转换为JSON字符串,并保存到指定的文件中 Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中 OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中 * writeValueAsString(obj):将对象转为json字符串*/ //使用writeValueAsString方法并打印字符串 String s = mapper.writeValueAsString(p); // System.out.println(s);//{"name":"张三","age":23,"gender":"男"} //使用writeValue方法将p对象写入到d:\\a.txt文件中 // mapper.writeValue(new File("d:\\a.txt"),p); //使用writeValue方法将p对象写入到d:\\b.txt文件输出流中 // mapper.writeValue(new FileWriter("d:/b.txt"),p); } @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 ObjectMapper mapper = new ObjectMapper(); //3.调用方法 String s = mapper.writeValueAsString(p); System.out.println(s);//{"name":"张三","age":23,"gender":"男","birthday":"2020-02-04"} } @Test //测试list集合 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(24); p1.setGender("男"); p1.setBirthday(new Date()); Person p2 = new Person(); p2.setName("王五"); p2.setAge(25); p2.setGender("女"); p2.setBirthday(new Date()); ArrayList list=new ArrayList(); list.add(p); list.add(p1); list.add(p2); //2.创建核心对象objectmapper ObjectMapper mapper = new ObjectMapper(); //3.调用方法 String s = mapper.writeValueAsString(list); System.out.println(s);//打印出来是数组的形式[{},{},{}] //[{"name":"张三","age":23,"gender":"男","birthday":"2020-02-04"}, // {"name":"李四","age":24,"gender":"男","birthday":"2020-02-04"}, // {"name":"王五","age":25,"gender":"女","birthday":"2020-02-04"}] } @Test //测试map集合 public void test4() throws Exception { Map<String,Object> map=new HashMap<String,Object>(); map.put("name","张三"); map.put("age",23); map.put("gender","男"); //2.创建核心对象objectmapper ObjectMapper mapper = new ObjectMapper(); //3.调用方法 String s = mapper.writeValueAsString(map); System.out.println(s);//{"gender":"男","name":"张三","age":23} } @Test //测试json转换成java对象 public void test5() throws Exception { //1.创建json字符串对象 String json="{\"gender\":\"男\",\"name\":\"张三\",\"age\":23}"; //2.创建核心对象objectmapper ObjectMapper mapper = new ObjectMapper(); //3.调用方法 Person person = mapper.readValue(json, Person.class); System.out.println(person);//Person{name=‘张三‘, age=23, gender=‘男‘, birthday=null} } }
原文:https://www.cnblogs.com/iceywu/p/12261994.html