码迷,mamicode.com
首页 > Web开发 > 详细

定制序列化之@JSONType的使用

时间:2018-06-24 20:54:36      阅读:2217      评论:0      收藏:0      [点我收藏+]

标签:imp   user   序列化   etag   oid   date()   package   ToJson   ret   

package com.ant.jdk8.json;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;

import java.util.Date;

@JSONType(includes = {"name","hireDate"},ignores = {"age"})
public class User {

    private String name;

    private int age;

    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date hireDate;

    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 Date getHireDate() {
        return hireDate;
    }

    public void setHireDate(Date hireDate) {
        this.hireDate = hireDate;
    }

    public User(String name, int age, Date hireDate) {
        this.name = name;
        this.age = age;
        this.hireDate = hireDate;
    }

    public User() {
    }
}
package com.ant.jdk8.json;

import com.alibaba.fastjson.JSON;

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

public class JsonDemo {

    public static void main(String[] args) {
        convertCollectionToJson();
    }

    public static void convertCollectionToJson(){
        List<User> userList = new ArrayList<>();
        userList.add(new User("tom",20,new Date()));
        userList.add(new User("jack",25,new Date()));
        String jsonString = JSON.toJSONString(userList,true);//true表示转换为带格式的json文本
        System.out.println(jsonString);
    }

}

技术分享图片

定制序列化之@JSONType的使用

标签:imp   user   序列化   etag   oid   date()   package   ToJson   ret   

原文地址:https://www.cnblogs.com/i-hard-working/p/9221483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!