jackson简单使用,对象转json,json转对象,json转list

简介: 添加jackson依赖: // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core compile group: 'com.

添加jackson依赖:

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.2'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.2'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.2'

看到fasterxml还以为找错依赖,还以为和com.alibaba:fastjson这个有啥联系,还以为是一个叫做jack的人写的。为啥有三个依赖,当发现大多数的框架都依赖于jackson来处理json转换的时候就自然而然的当做理所当然了。

 

 

POJO序列化为json字符串:

准备一个POJO:

@JsonIgnoreProperties(ignoreUnknown = true)
class User implements Serializable {
    private static final long serialVersionUID = -5952920972581467417L;
    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name=" + name +
                '}';
    }
}
  • @JsonIgnoreProperties(ignoreUnknown = true) 是为了反序列化的时候,如果遇到不认识的filed,忽略之
  • 无参构造函数是为了在反序列化的时候,jackson可以创建POJO实例
  • getter方法是为了序列化的时候,jackson可以获取filed值
  • toString是方便我自己debug看显示
  • 至于Serializable,习惯的给实体增加一个持久化的能力。

 

通过write来转化成jason字符串:

String expected = "{\"name\":\"Test\"}";
String test = mapper.writeValueAsString(new User("Test"));
Assert.assertEquals(expected, test);

通过read来parse json字符串为POJO对象:

User user = mapper.readValue(expected, User.class);
Assert.assertEquals("Test", user.getName());

 

 

jsonArray转换成Array数组:

String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]";
ArrayType arrayType = mapper.getTypeFactory().constructArrayType(User.class);
User[] users = mapper.readValue(expected, arrayType);
Assert.assertEquals("Ryan", users[0].getName());

 

jsonArray转换成List<>泛型:

expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class);
//the sieze of the list is dependon the str json length although the json content is not the POJO type maybe
List<User> userList = mapper.readValue(expected, listType);
Assert.assertEquals(3, userList.size());
Assert.assertNull(userList.get(0).getName());
Assert.assertEquals("Ryan",userList.get(2).getName());

 

jackson默认将对象转换为LinkedHashMap:

String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]";
ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
Object o = arrayList.get(0);
Assert.assertTrue(o instanceof LinkedHashMap);

 





唯有不断学习方能改变! -- Ryan Miao
目录
相关文章
|
前端开发 网络安全
layui如何实现添加数据时关闭页面层,并实时刷新表格数据?
layui如何实现添加数据时关闭页面层,并实时刷新表格数据?
|
容器
layui组件table定时刷新的解决方案
layui组件table定时刷新的解决方案
436 0
|
12月前
|
SQL JavaScript Java
Spring Boot 3 整合 Mybatis-Plus 实现数据权限控制
本文介绍了如何在Spring Boot 3中整合MyBatis-Plus实现数据权限控制,通过使用MyBatis-Plus提供的`DataPermissionInterceptor`插件,在不破坏原有代码结构的基础上实现了细粒度的数据访问控制。文中详细描述了自定义注解`DataScope`的使用方法、`DataPermissionHandler`的具体实现逻辑,以及根据用户的不同角色和部门动态添加SQL片段来限制查询结果。此外,还展示了基于Spring Boot 3和Vue 3构建的前后端分离快速开发框架的实际应用案例,包括项目的核心功能模块如用户管理、角色管理等,并提供Gitee上的开源仓库
2419 11
|
JSON JavaScript 前端开发
JavaScript实现字符串转json对象的方法
JavaScript实现字符串转json对象的方法
|
存储 Java 数据建模
Java零基础-三维数组详解!
【10月更文挑战第21天】Java零基础教学篇,手把手实践教学!
552 4
|
Oracle Java 关系型数据库
使用DataGrip链接达梦数据库
使用DataGrip链接达梦数据库
1231 0
|
存储 自然语言处理 关系型数据库
Elasticsearch 查询时 term、match、match_phrase、match_phrase_prefix 的区别
【7月更文挑战第3天】Elasticsearch 查询时 term、match、match_phrase、match_phrase_prefix 的区别
|
资源调度 Windows
Windows系统yarn : 无法加载文件
Windows系统yarn : 无法加载文件
542 0
|
JavaScript
使用layui checkbox复选框样式实现单选功能
使用layui checkbox复选框样式实现单选功能
788 0
|
Java Maven 开发工具
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)1
【IntelliJ IDEA】使用Maven方式构建Spring Boot Web 项目(超详细)
597 2

热门文章

最新文章