添加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);

最新文章

  1. hg0088新2网址
  2. Oracle case when的用法
  3. MVC自动生成数据库【Code-FIrst方式】
  4. Jenkins持续集成
  5. [转载]CRect::DeflateRect
  6. Cocos2d-x Application Wizard for Visual Studio User Guide
  7. Python之路【第五篇】:面向对象及相关
  8. Java 多线程 锁 存款 取款
  9. HDU 4622 Reincarnation(SAM)
  10. [GIT] warning: LF will be replaced by CRLF问题解决方法
  11. The Priest Mathematician
  12. ActiveReports 9实战教程(2): 准备数据源(设计时、运行时)
  13. (转)Vi命令详解
  14. 云服务器ECS优惠券 阿里云 ecs 5折优惠码 阿里云5折优惠码 阿里云5折推荐码 阿里云优惠码 阿里云的5折优惠券 阿里云服务器购买优惠码 服务器购买优惠码
  15. Excel uploading date format
  16. 如何将一段文本编译成C#内存程序的过程
  17. 测试那些事儿—selenium自动化实战之登录验证码处理
  18. 如何应用ML的建议-上
  19. 2018.08.04 洛谷P3380 【模板】二逼平衡树(树套树)
  20. java第三次试验报告

热门文章

  1. iOS可视化动态绘制八种排序过程
  2. JavaScript Object对象
  3. Hawk 7. 常见问题
  4. 设计模式之行为类模式大PK
  5. JavaScript实现DOM对象选择器
  6. 「译」JUnit 5 系列:条件测试
  7. [算法]——快速排序(Quick Sort)
  8. java中易错点(一)
  9. jQuery禁用或启用
  10. Atitit.研发团队与公司绩效管理的原理概论的attilax总结