使用fastjson
导包
implementation 'com.alibaba:fastjson:1.2.78'
代码
@Test
void dtoEntityTest() {
UserDTO user = new UserDTO("1", new Date(System.currentTimeMillis()), "REMORK");
// dto转entity
UserEntity userEntity = JSON.parseObject(JSON.toJSONString(user), UserEntity.class);
// entity转dto
UserDTO userDTO = JSON.parseObject(JSON.toJSONString(userEntity), UserDTO.class);
}
这篇博客介绍了如何使用Fastjson库在Java中进行对象与JSON字符串之间的转换。通过导入fastjson的依赖,博主展示了在测试方法中创建了一个UserDTO对象,然后将其转换为UserEntity对象,再将UserEntity转换回UserDTO。这个过程演示了Fastjson在Java对象转换中的基本用法。
3442

被折叠的 条评论
为什么被折叠?



