java使用jackson生成和解析JSON
1.导包
2.生成json和解析json
package test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import domain.Person; import org.junit.Test; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.*; /** * 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字符串 * 使用jackSon生成解析jsom * @date 2019/10/5 15:09 */ public class Test1 { @Test//生成json public void test() throws IOException { Person p=new Person(); p.setName("解析json"); p.setAge(13); p.setGender("男"); ObjectMapper mapper=new ObjectMapper();//先创建objmapper的对象 String string = mapper.writeValueAsString(p); /* *mapper.writeValue(参数1,obj): 1.File:将obj对象转换为JSON字符串,并保存到指定的文件中 * 2.Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中 * 3.OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中*/ System.out.println(string);//{"name":"解析json","age":13,"gender":"男"} // 1.File:将obj对象转换为JSON字符串,并保存到指定的文件中 mapper.writeValue(new File("D://a.txt"),p); // 2.Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中 mapper.writeValue(new FileWriter("D://b.txt"),p); } @Test//json注解 public void test1() throws JsonProcessingException { Person p=new Person(); p.setName("解析json"); p.setAge(13); p.setGender("男"); p.setDate(new Date()); ObjectMapper mapper=new ObjectMapper();//先创建objmapper的对象 String string = mapper.writeValueAsString(p); System.out.println(string); } @Test//json复杂转换 public void test2() throws JsonProcessingException { Person p=new Person(); p.setName("解析json"); p.setAge(13); p.setGender("男"); p.setDate(new Date()); Person p1=new Person(); p.setName("解析json"); p.setAge(13); p.setGender("男"); p.setDate(new Date()); Person p2=new Person(); p.setName("解析json"); p.setAge(13); p.setGender("男"); p.setDate(new Date()); //集合 List<Person> PS=new ArrayList<>(); PS.add(p); PS.add(p1); PS.add(p2); ObjectMapper mapper=new ObjectMapper();//先创建objmapper的对象 String string = mapper.writeValueAsString(PS); System.out.println(string); } @Test//json MAP生成json public void test3() throws JsonProcessingException { //创建map对象 Map<String,Object> map=new HashMap<>(); map.put("name","map"); map.put("age",11); map.put("gender","男"); ObjectMapper mapper=new ObjectMapper();//先创建objmapper的对象 String string = mapper.writeValueAsString(map); System.out.println(string); } @Test//json字符串转换为java对象 public void test4() throws IOException { String json="{\"gender\":\"男\",\"name\":\"map\",\"age\":11}"; ObjectMapper mapper=new ObjectMapper(); Person person = mapper.readValue(json, Person.class);//有了对象就可以调用getXx和setXxx方法了 System.out.println(person);//打印一个person对象 Person{name='map', age=11, gender='男', date=null} //这是一个json字符串{"name":"解析json","age":13,"gender":"男"} } }
3.person类
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 转换时忽略 date字段 @JsonFormat(pattern = "yyyy-MM-dd") private Date date; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", date=" + date + '}'; } 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; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }