当前位置:首页 » 编程语言 » java对象转字符串

java对象转字符串

发布时间: 2023-06-26 22:56:01

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

先给你一个正确的方法:

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

我的代码如下:

privateTimestamptime;


publicTimestampgetTime(){
returntime;
}

publicvoidsetTime(Timestamptime){
this.time=time;
}

测试方法:

publicstaticvoidmain(String[]args)throwsJsonProcessingException,ParseException{
Timestamptimestamp=newTimestamp(System.currentTimeMillis());
AccountInfoaccountInfo=newAccountInfo();
accountInfo.setTime(timestamp);

ObjectMappermapper=newObjectMapper();
mapper.setDateFormat(newSimpleDateFormat("yyyy-MM-ddhh:mm:ss"));
Strings=mapper.writeValueAsString(accountInfo);
System.out.println(s);

longtime=DateUtils.parseDate("1987-06-0400:00:001","yyyy-MM-ddhh:mm:ss").getTime();
Stringdate=DateUtils.parseDate("1987-06-0400:00:01","yyyy-MM-ddhh:mm:ss").toString();
System.out.println(date);


timestamp=Timestamp.valueOf("1987-06-0400:00:00");
System.out.println(timestamp);
accountInfo=newAccountInfo();
accountInfo.setTime(timestamp);

mapper=newObjectMapper();
mapper.setDateFormat(newSimpleDateFormat("yyyy-MM-ddhh: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-1601:27:16"}
ThuJun0400:00:01CDT1987
1987-06-0400:00:00.0
{"openId":null,"token":null,"ip":null,"account":null,"phone":null,"email":null,"platformType":0,"time":"1987-06-0412: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小时计进即可。

Ⅱ java 怎么将对象转化为字符串数组

不懂你说什么,不是什么对象都能转成字符串数组的,你是不是说的list转数组
List<ResourceType> resourceTypes=resourceTypeService.findAll();
ResourceType[] resourceTypeArray=(ResourceType[]) resourceTypes.toArray();

Ⅲ java中如何将对象转成json格式字符串

用Gson转换就行了,需要下载jar包

例子:

Personperson=newPerson();
Gsongson=newGson();
Stringjson=gson.toJson(person);


热点内容
中国十大解压电影 发布:2025-04-23 06:13:07 浏览:582
产品直播脚本范文例子 发布:2025-04-23 06:10:24 浏览:312
安卓id加密 发布:2025-04-23 06:10:23 浏览:388
python行内if 发布:2025-04-23 06:10:20 浏览:219
ubuntu编译32位程序 发布:2025-04-23 06:10:20 浏览:959
什么在资源配置中起宏观调控作用 发布:2025-04-23 06:05:25 浏览:723
换手机怎么把安卓app移到苹果手机 发布:2025-04-23 06:05:20 浏览:899
java的时间格式 发布:2025-04-23 06:04:39 浏览:975
美团安卓哪里再次配送 发布:2025-04-23 05:56:07 浏览:978
油画价格算法 发布:2025-04-23 05:54:37 浏览:532