将java对象转换成json字符串_java ObjectMapper 将对象转换成json字符串问题

展开全部

先给你一个正确的e5a48de588b63231313335323631343130323136353331333366303138方法:

1,把bean里面的get方法上面的格式去掉

我的代码如下:private Timestamp time;

public Timestamp getTime() {

return time;

}

public void setTime(Timestamp time) {

this.time = time;

}

测试方法:public static void main(String[] args) throws JsonProcessingException, ParseException {

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

AccountInfo accountInfo = new AccountInfo();

accountInfo.setTime(timestamp);

ObjectMapper mapper = new ObjectMapper();

mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));

String s = mapper.writeValueAsString(accountInfo);

System.out.println(s);

long time = DateUtils.parseDate("1987-06-04 00:00:001","yyyy-MM-dd hh:mm:ss").getTime();

String date = DateUtils.parseDate("1987-06-04 00:00:01","yyyy-MM-dd hh:mm:ss").toString();

System.out.println(date);

timestamp = Timestamp.valueOf("1987-06-04 00:00:00");

System.out.println(timestamp);

accountInfo = new AccountInfo();

accountInfo.setTime(timestamp);

mapper = new ObjectMapper();

mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"));

s = mapper.writeValueAsString(accountInfo);

System.out.println(s);

}

输出结果:{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"2018-10-16 01:27:16"}

Thu Jun 04 00:00:01 CDT 1987

1987-06-04 00:00:00.0

{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"1987-06-04 12:00:00"}

不过这里有一个小问题,因为你是使用的yyyy-MM-dd hh:mm:ss 格式,而hh表示按12小时计时,所以1987-06-04 00:00:00会在json中表示为1987-06-04 12:00:00,你可以换成yyyy-MM-dd HH:mm:ss按24小时计进即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值