天天看點

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;