如何在Java中使用flexjson通过@JSON注释控制序列化?
@JSON注释用于通过JSONSerializer 类在序列化过程,以排除或包括的字段。我们可以使用JSONSerializer 类的serialize()方法对目标实例执行浅化序列化。
语法
@Retention(value=RUNTIME)
@Target(value={FIELD,TYPE,METHOD})
public @interface JSON示例
import flexjson.JSONSerializer;
import flexjson.JSON;
public class JSONAnnotationTest {
public static void main(String[] args) {
JSONSerializer serializer = new JSONSerializer().prettyPrint(true);
Employee emp = new Employee("Raja", "Ramesh", 30, "Hyderabad");
String jsonStr = serializer.serialize(emp);
System.out.println(jsonStr);
}
}
//员工阶层
class Employee {
private String firstName, lastName, address;
private int age;
public Employee(String firstName, String lastName, int age, String address) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.address = address;
}
public String getFirstName() {
return firstName;
} @JSON(include=false)
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
@JSON(include=false) public String getAddress() {
return address;
}
}输出结果
{
"age": 30,
"class": "Employee",
"firstName": "Raja"
}热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短