Java 进阶 & JackJson 的使用

需要包:
jackson-core-2.2.3.jar(核心jar包)
jackson-annotations-2.2.3.jar(该包提供Json注解支持)
jackson-databind-2.2.3.jar

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.1</version>
</dependency>
1、指定对象Class转成 json字符串
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
user.setAge(20);

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));

/**

  • ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中实现。
  • ObjectMapper有多个JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介质中。
  • writeValue(File arg0, Object arg1)把arg1转成json序列,并保存到arg0文件中。
  • writeValue(OutputStream arg0, Object arg1)把arg1转成json序列,并保存到arg0输出流中。
  • writeValueAsBytes(Object arg0)把arg0转成json序列,并把结果输出成字节数组。
  • writeValueAsString(Object arg0)把arg0转成json序列,并把结果输出成字符串。
    */
    ObjectMapper mapper = new ObjectMapper();

//User类转JSON

{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);

输出:
{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}

2、List集合转化成json字符串
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);

输出:

[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]
3、Json字符串转化成指定Class类
String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";
/**
 * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
 */
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);

输出:

User{name='小民aa', age=25, birthday=Tue Oct 01 00:00:00 CST 1996, email='xiaomin@sina.com'}
4、Json字符串转化成集合List

方法一:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);
//如果是Map类型  mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);  
List<Bean> lst =  (List<Bean>)mapper.readValue(jsonString, javaType);

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
List<Bean> beanList = mapper.readValue(jsonString, new TypeReference<List<Bean>>() {});

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Jackson使用规范以及代码示例 依赖包 Maven依赖: org.codehaus.jackson jacks...
    山石水寿阅读 4,729评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,935评论 6 342
  • 她全神贯注的盯着眼前的小珠子,然后一颗颗的将不同的颜色分类,放好,然后拿起细绳一点点往珠子的洞眼里穿。刚开始有点费...
    莉莉安蓁妮阅读 379评论 0 0
  • 最近看了日本史学家宫崎市定先生的《中国史》,阅读的过程有过一次小的纠结,即读着读着甚至忘记了真正的历史是什么样了,...
    慧飞的鱼阅读 469评论 0 0