JSON解析器Jackson
JSON解析器:常见的解析器:Jsonlib,Gson,fastjson,jacksonn
java对象转为JSON
使用步骤
- 导入jackson的相关jar包
- 创建Jackson核心对象 ObjectMapper
- 调用objectMapper的相关方法进行转换
转换方法
- writeValue(参数1,Obj):
- 参数1:
- File:将obj对象转换为JSON字符串 并保存到文件中
- Writer:将obj对象转换为JSON字符串 并将json数据填充到字符输出流中
- OutputStream:将Obj对象转换为JSON字符串 并将json数据填充到字节输出流中
- 参数1:
- writeCalueAsString(obj):将对象转为json字符串
java代码
package com.bai.test; import com.bai.domain.Person; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; public class JacksonTest { //java对象转为JSON字符串 @Test public void test01() throws Exception { //创建person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setGender("男"); //创建Jackson的核心对象 ObjectMapper ObjectMapper mapper = new ObjectMapper(); //3转换 String json = mapper.writeValueAsString(p); //{"name":"张三","age":23,"gender":男} System.out.println(json); } }
JSON转为java对象_注解
- @JsonIgnore:排除属性
- @JsonFormat:属性值得格式化
实体类
package com.bai.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 + '\'' + '}'; } }
测试类
@Test public void test02() throws Exception { //创建person对象 Person p = new Person(); p.setName("张三"); p.setAge(23); p.setGender("男"); p.setBirthday(new Date()); //转换 ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(p); System.out.println(json); }
运行结果
复杂java对象转换
1.List
测试类
@Test public void test03() throws Exception { //创建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集合 ArrayList<Person> ps = new ArrayList<Person>(); ps.add(p); ps.add(p1); ps.add(p2); //2.转换 ObjectMapper map = new ObjectMapper(); String json = map.writeValueAsString(ps); System.out.println(json); }
2.Map
测试类
@Test public void test04() throws Exception { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("name","张三"); map.put("age",23); map.put("gender","男"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(map); System.out.println(json); } }
JSON转为java对象
- 导入jackson的相关jar包
- 创建Jackson核心对象 ObjectMapper
- 调用ObjectMapper的相关方法进行转换
- readValue(json字符串数据,class)
测试类
@Test public void test05() throws Exception { //初始值JSON字符串 String json="{\"gender\":\"男\",\"name\":\"张三\",\"age\":23}"; //创建ObjectMapper对象 ObjectMapper mapper = new ObjectMapper(); //转换为java对象 Person对象 Person person = mapper.readValue(json, Person.class); System.out.println(person); }
运行结果