Jackjson数据解析

xiaoxiao2021-02-28  11

title: Jackjson数据解析案例 date: 2018-2-4 23:12:39 categories: Struts2 tags: Struts2


xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多案例、资料请联系QQ:1280023003

模板类

package com.echo.daomian; import java.util.Date; import org.codehaus.jackson.map.annotate.JsonFilter; /*@JsonIgnoreProperties({"id","birthday"})*/ @JsonFilter("student") public class Student { private int id; private String name; private int age; // @JsonIgnore//让下面的时间属性不出现在json中 private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

数据解析测试类

package com.echo.json; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ser.FilterProvider; import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter; import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider; import org.junit.Test; import com.echo.daomian.Student; public class JackjsonTest { @Test public void jsonTest() throws JsonGenerationException, JsonMappingException, IOException{ Student s = new Student(); s.setId(1); s.setAge(20); s.setName("echo"); s.setBirthday(new Date()); ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); String json = mapper.writeValueAsString(s); System.out.println(json); } @Test public void jsonTestArray() throws JsonGenerationException, JsonMappingException, IOException{ Student s = new Student(); s.setId(1); s.setAge(20); s.setName("echo"); s.setBirthday(new Date()); Student s1 = new Student(); s1.setId(1); s1.setAge(21); s1.setName("echo"); s1.setBirthday(new Date()); List<Student> list = new ArrayList<>(); list.add(s1); list.add(s); ObjectMapper mapper = new ObjectMapper(); /*FilterProvider fp = new SimpleFilterProvider().addFilter("student", SimpleBeanPropertyFilter.filterOutAllExcept("id","birthday"));//设置这个后json数据中仅包含id和birthday */ FilterProvider fp = new SimpleFilterProvider().addFilter("student", SimpleBeanPropertyFilter.serializeAllExcept("id","birthday"));//设置这个后json数据中不包含id和birthday mapper.setFilters(fp); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); String json = mapper.writeValueAsString(list); System.out.println(json); } }

时间格式出现问题

ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); String json = mapper.writeValueAsString(s); 使用json对象格式化时间

如果不想某个数据出现在json中

方式一:

@JsonIgnore//让下面的时间属性不出现在json中 private Date birthday;

方式二:

@JsonIgnoreProperties({"id","birthday"}) public class Student { 直接在类名上加入该注解即可

方式三:特别推荐

ObjectMapper mapper = new ObjectMapper(); /*FilterProvider fp = new SimpleFilterProvider().addFilter("student", SimpleBeanPropertyFilter.filterOutAllExcept("id","birthday"));//设置这个后json数据中仅包含id和birthday */ FilterProvider fp = new SimpleFilterProvider().addFilter("student", SimpleBeanPropertyFilter.serializeAllExcept("id","birthday"));//设置这个后json数据中不包含id和birthday mapper.setFilters(fp); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); String json = mapper.writeValueAsString(list);
转载请注明原文地址: https://www.6miu.com/read-1650124.html

最新回复(0)