JSON忽略字段

得不到的永遠在騷動-json如何忽略字段!

前言:在工作中經常會使用到JSON格式的數據,在我們將對象轉化為JSON時,有時不需要全部的字段;那我們該怎麼處理呢?


下面給大家介紹幾種常見的忽略對象屬性的方法!

好了廢話不多BB,直接開碼!

JSON忽略字段


transient關鍵字

使用方法:可以在不需要的字段前面加上transient修飾

<code>public class JosnIgnoreTest {
public static void main(String[] args) {
Person person = new Person();
person.setName("zerocode");
person.setAge(18);
person.setPwd("123456");
System.out.println(JSON.toJSONString(person)); //輸出結果{"age":18,"name":"zerocode"}
}
}

class Person{
private String name;
private int age;
private transient String pwd;
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 getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}

}
​/<code>

使用阿里巴巴 fastjson工具包

使用方式:

  1. @JSONField(serialize=false)註解import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.annotation.JSONField;

    public class JosnIgnoreTest {

    public static void main(String[] args) {
    Person person = new Person();
    person.setName("zerocode");
    person.setAge(18);
    person.setPwd("123456");
    System.out.println(JSON.toJSONString(person));//輸出結果{"age":18,"name":"zerocode"}
    }


    }

    class Person{
    private String name;
    private int age;
    @JSONField(serialize=false)
    private String pwd;
    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 getPwd() {
    return pwd;
    }
    public void setPwd(String pwd) {
    this.pwd = pwd;
    }

    }
  2. 使用SimplePropertyPreFilter
<code>import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;

public class JosnIgnoreTest {

public static void main(String[] args) {
Person person = new Person();
person.setName("zerocode");
person.setAge(18);
person.setPwd("123456");

SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("pwd");

System.out.println(JSONObject.toJSONString(person, filter));//輸出結果{"age":18,"name":"zerocode"}
}

}

class Person {
private String name;
private int age;
private String pwd;

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 getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

}
​/<code>


使用谷歌gson工具包

使用方式:

  1. @Expose 註解(需要的字段添加此註解,忽略的字段不添加)import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    import com.google.gson.annotations.Expose;

    public class JosnIgnoreTest2 {

    public static void main(String[] args) {
    Person person = new Person();
    person.setName("zerocode");
    person.setAge(18);
    person.setPwd("123456");

    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    System.out.println(gson.toJson(person));
    }
    }

    class Person {
    @Expose
    private String name;
    @Expose
    private int age;
    private String pwd;

    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 getPwd() {
    return pwd;
    }

    public void setPwd(String pwd) {
    this.pwd = pwd;
    }
    }
  2. 使用特定修飾符
<code>import java.lang.reflect.Modifier;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JosnIgnoreTest {

public static void main(String[] args) {
Person person = new Person();
person.setName("zerocode");
person.setAge(18);
person.setPwd("123456");
person.setIdCard("6666");

Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.PUBLIC).create();//不展示public修飾胡屬性
System.out.println(gson.toJson(person));//{"name":"zerocode","age":18,"pwd":"123456"}

}
}

class Person {
private String name;
private int age;
private String pwd;

public String idCard;

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 getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public String getIdCard() {
return idCard;
}

public void setIdCard(String idCard) {
this.idCard = idCard;
}

}
​/<code>



分享到:


相關文章: