既然你看到这篇文章相信你已经了解JSON的好处了,那么废话不多说直接进入主题。
Jackson是java中众多json处理工具的一个,比起常见的Json-lib,Gson要快一些。
Jackson的官网:http://jackson.codehaus.org/
里面可以下载Jackson的Jar包 注意jackson依赖:Apache的commons-loggin。
下面聊一下Jackson的常见用法==================================
Bean----->JSON
- public static String beanToJson(Object obj) throws IOException {
- // 这里异常都未进行处理,而且流的关闭也不规范。开发中请勿这样写,如果发生异常流关闭不了
- ObjectMapper mapper = CommonUtil.getMapperInstance(false);
- StringWriter writer = new StringWriter();
- JsonGenerator gen = new JsonFactory().createJsonGenerator(writer);
- mapper.writeValue(gen, obj);
- gen.close();
- String json = writer.toString();
- writer.close();
- return json;
- }
- public static String beanToJson(Object obj) throws IOException {
- // 这里异常都未进行处理,而且流的关闭也不规范。开发中请勿这样写,如果发生异常流关闭不了
- ObjectMapper mapper = CommonUtil.getMapperInstance(false);
- StringWriter writer = new StringWriter();
- JsonGenerator gen = new JsonFactory().createJsonGenerator(writer);
- mapper.writeValue(gen, obj);
- gen.close();
- String json = writer.toString();
- writer.close();
- return json;
- }
JSON------>Bean
- public static Object jsonToBean(String json, Class<?> cls) throws Exception {
- Object vo = mapper.readValue(json, cls);
- return vo;
- }
- public static Object jsonToBean(String json, Class<?> cls) throws Exception {
- Object vo = mapper.readValue(json, cls);
- return vo;
- }
好了方法写完了咱们测试一下吧 看看他是否支持复杂类型的转换
- public static void main(String[] args) throws Exception {
- // 准备数据
- List<Person> pers = new ArrayList<Person>();
- Person p = new Person("张三", 46);
- pers.add(p);
- p = new Person("李四", 19);
- pers.add(p);
- p = new Person("王二麻子", 23);
- pers.add(p);
- TestVo vo = new TestVo("一个容器而已", pers);
- // 实体转JSON字符串
- String json = beanToJson(vo);
- System.out.println("Bean>>>Json----" + json);
- // 字符串转实体
- TestVo vo2 = (TestVo)jsonToBean(json, TestVo.class);
- System.out.println("Json>>Bean--与开始的对象是否相等:" + vo2.equals(vo));
- }
- public static void main(String[] args) throws Exception {
- // 准备数据
- List<Person> pers = new ArrayList<Person>();
- Person p = new Person("张三", 46);
- pers.add(p);
- p = new Person("李四", 19);
- pers.add(p);
- p = new Person("王二麻子", 23);
- pers.add(p);
- TestVo vo = new TestVo("一个容器而已", pers);
- // 实体转JSON字符串
- String json = beanToJson(vo);
- System.out.println("Bean>>>Json----" + json);
- // 字符串转实体
- TestVo vo2 = (TestVo)jsonToBean(json, TestVo.class);
- System.out.println("Json>>Bean--与开始的对象是否相等:" + vo2.equals(vo));
- }
输出结果
Bean>>>Json----{"voName":"一个容器而已","pers":[{"name":"张三","age":46},{"name":"李四","age":19},{"name":"王二麻子","age":23}]}
Json>>Bean--与开始的对象是否相等:true
从结果可以看出从咱们转换的方法是对的,本文只是对Jackson的一个最简单的使用介绍。接下来的几篇文章咱们深入研究一下这玩意到底有多强大!
下面是本人涉及到的Bean 以及 CommonUtil
- public class CommonUtil {
- private static ObjectMapper mapper;
- /**
- * 一个破ObjectMapper而已,你为什么不直接new 还搞的那么复杂。接下来的几篇文章我将和你一起研究这个令人蛋疼的问题
- *
- * @param createNew
- * 是否创建一个新的Mapper
- * @return
- */
- public static synchronized ObjectMapper getMapperInstance(boolean createNew) {
- if (createNew) {
- return new ObjectMapper();
- } else if (mapper == null) {
- mapper = new ObjectMapper();
- }
- return mapper;
- }
- }
- public class CommonUtil {
- private static ObjectMapper mapper;
- /**
- * 一个破ObjectMapper而已,你为什么不直接new 还搞的那么复杂。接下来的几篇文章我将和你一起研究这个令人蛋疼的问题
- *
- * @param createNew
- * 是否创建一个新的Mapper
- * @return
- */
- public static synchronized ObjectMapper getMapperInstance(boolean createNew) {
- if (createNew) {
- return new ObjectMapper();
- } else if (mapper == null) {
- mapper = new ObjectMapper();
- }
- return mapper;
- }
- }
- public class Person {
- private String name;
- private int age;
- public Person() {
- }
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- Person other = (Person) obj;
- if (age != other.age) {
- return false;
- }
- if (name == null) {
- if (other.name != null) {
- return false;
- }
- } else if (!name.equals(other.name)) {
- return false;
- }
- return true;
- }
- }
- public class Person {
- private String name;
- private int age;
- public Person() {
- }
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- Person other = (Person) obj;
- if (age != other.age) {
- return false;
- }
- if (name == null) {
- if (other.name != null) {
- return false;
- }
- } else if (!name.equals(other.name)) {
- return false;
- }
- return true;
- }
- }
- public class TestVo {
- private String voName;
- private List<Person> pers;
- public TestVo() {
- }
- public TestVo(String voName, List<Person> pers) {
- super();
- this.voName = voName;
- this.pers = pers;
- }
- public String getVoName() {
- return voName;
- }
- public void setVoName(String voName) {
- this.voName = voName;
- }
- public List<Person> getPers() {
- return pers;
- }
- public void setPers(List<Person> pers) {
- this.pers = pers;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- TestVo other = (TestVo) obj;
- if (pers == null) {
- if (other.pers != null) {
- return false;
- }
- } else if (pers.size() != other.pers.size()) {
- return false;
- } else {
- for (int i = 0; i < pers.size(); i++) {
- if (!pers.get(i).equals(other.pers.get(i))) {
- return false;
- }
- }
- }
- if (voName == null) {
- if (other.voName != null) {
- return false;
- }
- } else if (!voName.equals(other.voName)) {
- return false;
- }
- return true;
- }
- }