JSON 序列化key排序问题和序列化大小写问题

1. JSON 序列化key排序问题(fastjson)

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
//创建学生对象
Student student = new Student();
student.setName("小明");
student.setSex(1);
student.setAge(18);
//序列化 json key按字典排序
System.out.println(JSONObject.toJSONString(student ,SerializerFeature.MapSortField));
//过滤不要的key age
 System.out.println(JSONObject.toJSONString(student , new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                if ("age".equals(name)) {
                    return false;
                }
                return true;
            }
        }, SerializerFeature.MapSortField));

2. JSON 序列化大小写问题(fastjson)

//学生类
import com.alibaba.fastjson.annotation.JSONField;
public class Student {
    private String name;
    private Integer sex;
    private Integer age;
    @JSONField(name = "Name")		//用于序列化成json,key Name
    public String getName() {
        return name;
    }
    @JSONField(name = "Name")		////用于json(Name)反序列化成学生对象
    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

3. jackson 序列化大小写问题(@ResponseBody和@RequestBody中的序列化和反序列化就是用的jackson)

 //学生类
import com.fasterxml.jackson.annotation.JsonProperty;
public class Student {
    @JsonProperty("Name")
    private String name;
    private Integer sex;
    private Integer age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getSex() {
        return sex;
    }
    public void setSex(Integer sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

//自己测试下
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
@Test
public void test() throws Exception{
	 Student student = new Student();
	 student.setName("小明");
	 ObjectMapper MAPPER = new ObjectMapper();
	 //jackson序列化
	 String json = MAPPER.writeValueAsString(student);
	 System.out.println(json);
	 //jackson反序列化
	 Student student2 = MAPPER.readValue(json, Student.class);
	 System.out.println(student2.getName());
}

4. jackson 序列化null值的问题(fastjson序列化默认会去掉值为null的键值对)

//在学生类上加上这个
方式一:(已经过时的方法)
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
方式二:
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)

5. jackson 反序列化忽略多余的json字段

import com.fasterxml.jackson.databind.DeserializationFeature;
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

6. jackson 序列化忽略多余的json字段

方式一:
@JsonIgnoreProperties:该注解将在类曾级别上使用以忽略json属性。在下面的栗子中,我们将从albums的dataset中忽略“tag”属性;
@JsonIgnoreProperties({ "tags" })

方式二:
@JsonIgnore:该注释将在属性级别上使用以忽略特定属性;get方法上
@JsonIgnore 

7. jackson 常用注解

@JsonAlias("Name")     		反序列化时生效
private String name;
@JsonProperty("Name")		反序列化和序列化都时生效
private String name;

你可能感兴趣的:(json,jackson,序列化大小写问题)