fastjson不序列化属性

前端之家收集整理的这篇文章主要介绍了fastjson不序列化属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

fastjson 的github地址:

https://github.com/alibaba/fastjson

基本使用示例:

FastJsonDemo.java

import com.alibaba.fastjson.JSON;

/**
 * fastjson不序列化属性示例
 *
 * @author Kevin
 * @date 2016-4-1
 */
public class FastJsonDemo {

	public static void main(String[] args) {
		Group group = new Group();
		group.setId(1L);
		group.setName("admin");

		User guestUser = new User();
		guestUser.setId(2L);
		guestUser.setName("guest");

		User rootUser = new User();
		rootUser.setId(3L);
		rootUser.setName("root");

		group.addUser(guestUser);
		group.addUser(rootUser);

		// Encode
		String jsonString = JSON.toJSONString(group);

		// jsonString的值为:
		// {"id":1}
	}
}

Group.java

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 用户组
 *
 * @author Kevin
 * @date 2016-4-1
 */
public class Group {
    private Long id;
    @JSONField(serialize = false)
    private String name;
    @JSONField(serialize = false)
    private List<User> users = new ArrayList<User>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void addUser(User user) {
        users.add(user);
    }
}

User.java

/**
 * 用户
 *
 * @author Kevin
 * @date 2016-4-1
 */
public class User {

	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

猜你在找的Json相关文章