论坛首页 编程语言技术论坛

jackson 序列化格式化时间

浏览 3156 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-04-01  
public class User {

	private int id;
	
	private Date birthday;

	private double money;

	private String name;
	
	public User() {
	}

	public User(int id, String name, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.birthday = birthday;
	}
	
	public User(int id, String name, double money, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.money = money;
		this.birthday = birthday;
	}

	public Date getBirthday() {
		return birthday;
	}

	public int getId() {
		return id;
	}

	public double getMoney() {
		return money;
	}

	public String getName() {
		return name;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public void setMoney(double money) {
		this.money = money;
	}

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


		ObjectMapper mapper = new ObjectMapper();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		mapper.setDateFormat(format);
		User user = new User(1,"JACK",new Date());
		String outJson = mapper.writeValueAsString(user);
		System.out.println(outJson);


//结果{"id":1,"name":"JACK","money":0.0,"birthday":"2013-04-01"}

//objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,FALSE);
//禁止使用时间戳(数字),而使用[ISO-8601标准的符号,它得到类似的输出:“1970-01-01T00:00:00.000 +0000”。
   发表时间:2013-04-02  
大多数的格式化操作不是在JSON这一块完成的,则是在JAVA端完成,服务器端给什么,JSON传什么就是的了。
1 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics