本文主要简单的介绍了Jackson中的一个重要类ObjectMapper的用法。曾经以为这个类很难,但是真正操作了一遍之后发现,这个类的使用非常的简单。
简单使用的化要点不过就两个:
1 两个方法:
1.1 对象-->Json字符串 :writeValueAsString(Object obj);
无论是是POJO还是 集合和数组,都可以使用这个方法,将其对象转换为Json字符串。拿来就用。
1.2 Json字符串-->对象 :readValue(String json,Class clazz);
这个方法也不难,从json到简单POJO 或者 到 Map ,数组都没有问题;
比如:readValue(json,Person.class);readValue(json,Map.class);readValue(json,Person[].class)
只是要注意json到list的转换有点特别的就可以。
readValue(json,List.class); 该返回类型是:List<LinkedHashMap<String ,Object>>
所以,并不能通过readValue(json,List.class);来直接过得List<Person>
如果想得到,就要使用JavaType这个类了。
2 一个类:JavaType
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Person.class);
//这是什么意思呢? constructParametricType 见名思意:构造器 参数化 类型。--》也就是构造器的参数类型。
//对于本示例来说,就是List的构造器参数类型设置为:Person. 不再是默认的LinkedHashMap
List<Person> pList = mapper.readValue(json, javaType);
System.out.println(pList);
//输出结果:[cn.htb.Person@57e1b0c, cn.htb.Person@4232c52b]
System.out.println(pList.get(0).getName());
//输出结果:张三
重点内容差不多就上边那些了,下面是针对ObjectMapper的属性设置代码。
以及针对每个类型进行的示例:一共有9个,但是都很简单。且明确标有注释。
package cn.htb;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
*
* @Description 测试ObjectMapper的简单用法,
* 重点就是两个方法 :
* writeValueAsString(Object obj)
* readValue(String json,Class<T> clazz);
* 还有一个类:JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Person.class);
* @ClassName JsonTest
* @Author [浅醉]
* @Date 2019年1月13日 下午8:54:43
*/
public class JsonTest {
public static final ObjectMapper mapper =new ObjectMapper();
static{
/**ObjectMapper 一些属性的设置 */
//序列化策略:序列化的时候序列对象的所有属性
mapper.setSerializationInclusion(Include.ALWAYS);
//反序列化策略:反序列化的时候如果多了其他属性,不抛出异常
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//如果是空对象的时候,不抛异常
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//取消时间的转化格式,默认是时间戳(毫秒数),可以取消,同时需要设置要表现的时间格式
//取消默认的
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
//设置新的
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
/************************* 对象 -> json *******************************************/
//对象转为json
@Test
public void test1() throws JsonProcessingException{
Person p = new Person("1","zs",19,"1234","234");
String json = mapper.writeValueAsString(p);
System.out.println(json);
//输出结果:{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}
}
//对象转为json,并打印到文件中 和控制台上
@Test
public void test2() throws IOException{
Person p = new Person("1","zs",19,"1234","234");
//将json打印到json.txt中 :方法 writeValue(File file,Object value);
mapper.writeValue(new File("josn.txt"), p);
//将json输出到控制台上 方法 writeValue(OutputStream os,Object value);
mapper.writeValue(System.out, p);
//输出结果:{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}
}
//Map转为json
@Test
public void test3() throws IOException{
Map map = new HashMap();
map.put("name", "张三");
map.put("age", 19);
String json = mapper.writeValueAsString(map);
System.out.println(json);
//输出就结果:{"name":"张三","age":19}
}
//List集合转换为json
@Test
public void test4() throws IOException{
Person p = new Person("1","zs",19,"1234","234");
Person p2 = new Person("1","zs",19,"1234","234");
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p2);
String json = mapper.writeValueAsString(list);
System.out.println(json);
//输出结果:
//[{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"},
//{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}]
}
/************************* json -> 对象 *******************************************/
//json转换为对象
@Test
public void test5() throws IOException{
Person p = new Person("1","zs",19,"1234","234");
String json = mapper.writeValueAsString(p);
//json值:{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}
Person person = mapper.readValue(json,Person.class);
System.out.println(person);
//输出结果:cn.htb.Person@74650e52
}
//json转换为Map
@Test
public void test6() throws IOException{
Person p = new Person("1","zs",19,"1234","234");
String json = mapper.writeValueAsString(p);
//json值:{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}
Map map = mapper.readValue(json,Map.class);
System.out.println(map);
//输出结果:{id=1, name=zs, age=19, email=234, phone=1234}
}
//json转换为List<LinkedHashMap<String,Object>>
@Test
public void test7() throws IOException{
Person p = new Person("1","张三",15,"123","1234");
Person p2 = new Person("1","张三",15,"123","1234");
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p2);
String json = mapper.writeValueAsString(list);
//json值:
//[{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"},
//{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}]
List list2 = mapper.readValue(json,List.class);
System.out.println(list2);
//输出结果:[{id=1, name=张三, age=15, email=123, phone=1234},
// {id=1, name=张三, age=15, email=123, phone=1234}]
//如果这样写:
List<Person> list3 = mapper.readValue(json,List.class); //到这不会报错
//System.out.println(list3.get(0).getName()); //到这会报错
//java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to cn.htb.Person
//由此,虽然泛型List<Person>时没有错误,但是真正调用时就出错了。
//可见,readValue(json,List.class)返回类型是List<LinkedHashMap<String,Object>>
//那么如果想,让他真正返回List<Person>该怎么办呢?
//使用JavaType 如下一个示例
}
//json转换为List<Person>
@Test
public void test8() throws IOException{
Person p = new Person("1","张三",15,"123","1234");
Person p2 = new Person("1","张三",15,"123","1234");
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p2);
String json = mapper.writeValueAsString(list);
//json值:
//[{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"},
//{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}]
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Person.class);
//这是什么意思呢? constructParametricType 见名思意:构造器 参数化 类型。--》也就是构造器的参数类型。
//对于本示例来说,就是List的构造器参数类型设置为:Person. 不再是默认的LinkedHashMap
List<Person> pList = mapper.readValue(json, javaType);
System.out.println(pList);
//输出结果:[cn.htb.Person@57e1b0c, cn.htb.Person@4232c52b]
//此时在,调用试试
System.out.println(pList.get(0).getName());
//输出结果:张三
}
//json转换为Person[]
@Test
public void test9() throws IOException{
Person p = new Person("1","张三",15,"123","1234");
Person p2 = new Person("1","张三",15,"123","1234");
List<Person> list = new ArrayList<>();
list.add(p);
list.add(p2);
String json = mapper.writeValueAsString(list);
//json值:
//[{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"},
//{"id":"1","name":"zs","age":19,"email":"234","phone":"1234"}]
Person[] pArr = mapper.readValue(json, Person[].class);
System.out.println(pArr);
//输出结果 [Lcn.htb.Person;@305fd85d
System.out.println(pArr[0]);
//输出结果 cn.htb.Person@458c1321
Person person = pArr[0];
System.out.println(person.getName());
//输出结果 张三
}
}