Java之Jackson框架
在Jackson框架中,提供了三种方式用来处理JSON数据:
流式API
在该方式下,使用JsonParser读取JSON数据,使用JsonGenerator写JSON数据。这种方式性能最佳(最低开销、最快速度读/写,其他两种方式基于该方式实现)
public static void write2JsonByStreamApi() throws Exception {
JsonFactory jf = new JsonFactory();
JsonGenerator jg = jf.createGenerator(new File("D:/user.json"),
JsonEncoding.UTF8);
jg.writeStartObject(); jg.writeObjectFieldStart("users");
jg.writeStringField("name", "jackson");
jg.writeNumberField("age", 13);
jg.writeEndObject(); jg.writeEndObject();
jg.flush();
jg.close();
}
树模型
在该模式下,将JSON数据以树的方式存储在内存中,使用ObjectMapper读取JSON数据生成树,树是JsonNode节点的集合
数据绑定
在该模式下,可以方便的将JSON和POJO相互转化,数据的绑定有两种变体:
- 简单数据绑定
简单数据绑定是指Map、List、String、Numbers、Boolean、以及null之间的相互转化,其转化对应如下:
JSON Type | Java Type |
Object | LinkedHashMap<String,Object> |
Array | ArrayList<Object> |
String | String |
Number(整数) |
Integer、Long、BigInteger |
Number(小数) | Double |
True|False | Boolean |
Null | Null |
- 完整数据绑定
完整数据绑定是指任何Java Bean类型上述简单数据绑定进行转换
/*********************************************/
/**父母信息类*/
public class Parent {
private String fathername = "";
private String mothername = "";
public Parent() {
/**JSON串转为Java对象时调用无惨构造函数*/
}
public Parent(String fname,String mname) {
this.fathername = fname;
this.mothername = mname;
}
public String getFathername() {
return fathername;
}
public void setFathername(String fathername) {
this.fathername = fathername;
}
public String getMothername() {
return mothername;
}
public void setMothername(String mothername) {
this.mothername = mothername;
}
}
/**教师信息类*/
public class Teacher {
private String name = "";
private int age = 0;
public Teacher() {
/**JSON串转为Java对象时调用无惨构造函数*/
}
public Teacher(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/**学生信息类*/
public class Student {
private String name = "";
private String sex = "";
private int age = 0;
private Parent parent = null;
private Teacher[] teachers = null;
public Student() {
/**JSON串转为Java对象时调用无惨构造函数*/
}
public Student(String name,String sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public Teacher[] getTeachers() {
return teachers;
}
public void setTeachers(Teacher[] teachers) {
this.teachers = teachers;
}
}
/*********************************************/
public class JSonUtil {
private static ObjectMapper mapper = null;
static {
mapper = new ObjectMapper();
}
public static void write2Json(Object obj) throws IOException {
mapper.writeValue(System.out, obj);
}
public static Object json2Object(String json,Class<? extends Object> clazz) throws Exception {
return mapper.readValue(json, clazz);
}
public static void main(String []args) throws Exception {
/**Java对象转化为JSON串*/
Parent parent = new Parent("张大龙","张园园");
Teacher teacher_1 = new Teacher("李大成",46);
Teacher teacher_2 = new Teacher("王大虎",43);
Student student = new Student("张明","男",22);
student.setParent(parent);
student.setTeachers(new Teacher[]{teacher_1,teacher_2});
JSonUtil.write2Json(student);
/**JSON串转换为Java对象*/
String json = "{\"name\":\"张明\",\"sex\":\"男\",\"age\":22," +"\"parent\":{\"fathername\":\"张大龙\",\"mothername\":\"张园园\"}," + "\"teachers\":[{\"name\":\"李大成\",\"age\":46}," + "{\"name\":\"王大虎\",\"age\":43}]}";
Student student = (Student) JSonUtil.json2Object(json, Student.class);
System.out.println(student.getParent().getFathername());
}
}
/*************************输出结果*****************************/
{"name":"张明","sex":"男","age":22,"parent":{"fathername":"张大龙","mothername":"张园园"},"teachers":[{"name":"李大成","age":46},{"name":"王大虎","age":43}]}
在上面的示例中,需要注意的是:
- 将JSON串转为Java对象时,需要Java对象提供无参的构造函数,并且要求Java对象具有Java Bean性质,也就是说要有setter/getter方法
- 将Java对象转为JSON字符串时,JSON串Object的key名称取自Java对象的属性名称,如果想改变JSON串Object的key名称需要重写自己的序列化器,在解析时需要重写反序列化器,并且在序列化器中指定JSON串顺序
/**序列化器*/
public class StudentSerializer extends JsonSerializer<Student> {
@Override
public void serialize(Student value, JsonGenerator js, SerializerProvider provider) throws IOException,
JsonProcessingException {
// TODO Auto-generated method stub
js.writeStartObject(); js.writeObjectField("学生姓名", value.getName());
js.writeObjectField("性别", value.getSex());
js.writeObjectField("年龄", value.getAge());
js.writeFieldName("父母信息"); js.writeStartObject();
js.writeObjectField("父亲姓名", value.getParent().getFathername());
js.writeObjectField("母亲姓名", value.getParent().getMothername());
js.writeEndObject(); js.writeArrayFieldStart("教师信息");
Teacher [] teachers = value.getTeachers();
for(Teacher teacher : teachers) {
js.writeStartObject();
js.writeObjectField("教师姓名", teacher.getName());
js.writeObjectField("教师年龄", teacher.getAge());
js.writeEndObject();
}
js.writeEndArray(); js.writeEndObject();
}
}
/**反序列化器*/
public class StudentDeserializer extends JsonDeserializer<Student> {
@Override
public Student deserialize(JsonParser parser, DeserializationContext context)
throws IOException, JsonProcessingException {
// TODO Auto-generated method stub
JsonNode node = parser.getCodec().readTree(parser);
String name = node.get("学生姓名").asText();
String sex = node.get("性别").asText();
int age = node.get("年龄").asInt();
JsonNode pNode = node.get("父母信息");
String fathername = pNode.get("父亲姓名").asText();
String mothername = pNode.get("母亲姓名").asText();
Parent parent = new Parent();
parent.setFathername(fathername);
parent.setMothername(mothername);
JsonNode tNodes = node.get("教师信息");
ArrayList<Teacher> list = new ArrayList<Teacher>();
for(JsonNode tNode : tNodes) {
String teachername = tNode.get("教师姓名").asText();
int teacherage = tNode.get("教师年龄").asInt();
Teacher teacher = new Teacher();
teacher.setName(teachername);
teacher.setAge(teacherage);
list.add(teacher);
}
Student student = new Student();
student.setName(name);
student.setAge(age);
student.setSex(sex);
student.setParent(parent);
student.setTeachers((Teacher[]) list.toArray(new Teacher[]{}));
return student;
}
}
/**************************************************************/
public class JSonUtil {
private static ObjectMapper mapper = null;
static {
mapper = new ObjectMapper();
/**注册序列化器与反序列化器*/
StudentSerializer ser = new StudentSerializer();
StudentDeserializer deser = new StudentDeserializer();
SimpleModule module = new SimpleModule();
module.addSerializer(Student.class, ser);
module.addDeserializer(Student.class, deser);
mapper.registerModule(module);
}
public static void write2Json(Object obj) throws IOException {
mapper.writeValue(System.out, obj);
}
public static Object json2Object(String json,Class<? extends Object> clazz) throws Exception {
return mapper.readValue(json, clazz);
}
public static void main(String []args) throws Exception {
Parent parent = new Parent("张大龙","张园园");
Teacher teacher_1 = new Teacher("李大成",46);
Teacher teacher_2 = new Teacher("王大虎",43);
Student student = new Student("张明","男",22);
student.setParent(parent);
student.setTeachers(new Teacher[]{teacher_1,teacher_2});
JSonUtil.write2Json(student);
String json = "{\"学生姓名\":\"张明\",\"性别\":\"男\",\"年龄\":22," + "\"父母信息\":{\"父亲姓名\":\"张大龙\",\"母亲姓名\":\"张园园\"}," + "\"教师信息\":[{\"教师姓名\":\"李大成\",\"教师年龄\":46}," + "{\"教师姓名\":\"王大虎\",\"教师年龄\":43}]}";
Student stu = (Student) JSonUtil.json2Object(json, Student.class);
System.out.println(stu.getParent().getFathername());
}
}
在上面的序列化示例中,可以看出需要将自定义的序列化器与反序列化器注册到ObjectMapper中,在Jackson2.x框架中,提供了注解方式
/*************序列化对象添加注解***************/
@JsonSerialize(using = StudentSerializer.class)
@JsonDeserialize(using = StudentDeserializer.class)
public class Student {
private String name = "";
private String sex = "";
private int age = 0;
private Parent parent = null;
private Teacher[] teachers = null;
public Student() { }
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public Teacher[] getTeachers() {
return teachers;
}
public void setTeachers(Teacher[] teachers) {
this.teachers = teachers;
}
}
/***********************************************/
public class JSonUtil {
private static ObjectMapper mapper = null;
static {
mapper = new ObjectMapper();
}
public static void write2Json(Object obj) throws IOException {
mapper.writeValue(System.out, obj);
}
public static Object json2Object(String json,Class<? extends Object> clazz) throws Exception {
return mapper.readValue(json, clazz);
}
public static void main(String []args) throws Exception {
Parent parent = new Parent("张大龙", "张园园");
Teacher teacher_1 = new Teacher("李大成", 46);
Teacher teacher_2 = new Teacher("王大虎", 43);
Student student = new Student("张明", "男", 22);
student.setParent(parent);
student.setTeachers(new Teacher[] { teacher_1, teacher_2 });
JSonUtil.write2Json(student);
String json = "{\"学生姓名\":\"张明\",\"性别\":\"男\",\"年龄\":22,"
+ "\"父母信息\":{\"父亲姓名\":\"张大龙\",\"母亲姓名\":\"张园园\"},"
+ "\"教师信息\":[{\"教师姓名\":\"李大成\",\"教师年龄\":46},"
+ "{\"教师姓名\":\"王大虎\",\"教师年龄\":43}]}";
Student stu = (Student) JSonUtil.json2Object(json, Student.class);
System.out.println(stu.getParent().getFathername()); }
}
- 泛型的数据绑定
除绑定到POJO和简单类型外,还有一个额外的变型:绑定到泛型容器,由于所谓的类型擦除【Java采用向后兼容的方式实现泛型】,需要进行特殊处理,这时需要借助TypeReference类
/*********************************************/
public class PersonMsg {
private String name;
private String sex;
public PersonMsg() { }
public PersonMsg(String name,String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
/*********************************************/
public class JSonUtil {
private static ObjectMapper mapper = null;
static {
mapper = new ObjectMapper();
}
public static void write2Json(Object obj) throws IOException {
mapper.writeValue(System.out, obj);
}
public static Object json2Object(String json,TypeReference<Map<String,PersonMsg>> typeReference) throws Exception {
return mapper.readValue(json, typeReference);
}
public static void main(String []args) throws Exception {
PersonMsg p0 = new PersonMsg("liming", "man");
PersonMsg p1 = new PersonMsg("lixiang", "woman");
Map<String, PersonMsg> map = new HashMap<String, PersonMsg>();
map.put("liming", p0);
map.put("lixiang", p1);
JSonUtil.write2Json(map);
String json = "{\"liming\":{\"name\":\"liming\",\"sex\":\"man\"}," +
"\"lixiang\":{\"name\":\"lixiang\",\"sex\":\"woman\"}}";
Map<String,PersonMsg> pmap = (Map<String, PersonMsg>) JSonUtil.json2Object(json, new TypeReference<Map<String,PersonMsg>>(){});
}
}
Java之Jackson框架的更多相关文章
- Jackson 框架,轻易转换JSON
Jackson 框架,轻易转换JSON Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 前面有介绍过json-lib这个框架,在 ...
- Jackson 框架,轻易转换JSON【转】
Jackson 框架,轻易转换JSON Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 前面有介绍过json-lib这个框架,在 ...
- Java微服务框架一览
引言:本文首先简单介绍了微服务的概念以及使用微服务所能带来的优势,然后结合实例介绍了几个常见的Java微服务框架. 微服务在开发领域的应用越来越广泛,因为开发人员致力于创建更大.更复杂的应用程序,而这 ...
- Jackson 框架的高阶应用
Jackson 是当前用的比较广泛的,用来序列化和反序列化 json 的 Java 的开源框架.Jackson 社 区相对比较活跃,更新速度也比较快, 从 Github 中的统计来看,Jackson ...
- Java 使用 Jackson库 对 JavaMap 进行序列化反序列化
最近在用 java 处理一一些东西,发现 java 对对象进行序列化反序列化比起 python 来还是有些麻烦记录一下. 找了好几个库最后选择了 Jackson 感觉大家对它评价还不错. 将目标从 J ...
- 9个基于Java的搜索引擎框架
在这个信息相当繁杂的互联网时代,我们已经学会了如何利用搜索引擎这个强大的利器来找寻目标信息,比如你会在Google上搜索情人节如何讨女朋友欢心,你也会在百度上寻找正规的整容医疗机构(尽管有很大一部分广 ...
- cache4j轻量级java内存缓存框架,实现FIFO、LRU、TwoQueues缓存模型
简介 cache4j是一款轻量级java内存缓存框架,实现FIFO.LRU.TwoQueues缓存模型,使用非常方便. cache4j为java开发者提供一种更加轻便的内存缓存方案,杀鸡焉用EhCac ...
- Java 线程池框架核心代码分析--转
原文地址:http://www.codeceo.com/article/java-thread-pool-kernal.html 前言 多线程编程中,为每个任务分配一个线程是不现实的,线程创建的开销和 ...
- 5个强大的Java分布式缓存框架推荐
在开发中大型Java软件项目时,很多Java架构师都会遇到数据库读写瓶颈,如果你在系统架构时并没有将缓存策略考虑进去,或者并没有选择更优的 缓存策略,那么到时候重构起来将会是一个噩梦.本文主要是分享了 ...
随机推荐
- 转:Java NIO
Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.本系列教程将有助于你学习和理解Java NIO.感谢并发编程网的翻译和投递 ...
- 求强连通块_Tarjan算法_C++
好久没有写算法了,就放一个 Tarjan 上来凑凑数哈 强连通块由若干个点组成,任意点与点之间可以之间或间接到达,显然可以看作一个环 下面是伪代码 强记:dfn为时间不变,low取最小,下一个dfn有 ...
- 【dlib代码解读】人脸检测器的训练【转】
转自:http://blog.csdn.net/elaine_bao/article/details/53046542 版权声明:本文为博主原创文章,转载请注明. 目录(?)[-] 综述 代码解读 ...
- python bisect模块二分法查找
#!/usr/bin/env python # encoding: utf-8 import bisect import sys #将一个元素插入到一个有序列表的合适位置 #使用这个模块的函数前先确保 ...
- 使用 JavaScript 将网站后台的数据变化实时更新到前端-【知乎总结】
问: 难道只能设置定时器每隔一秒通过 Ajax 向后台请求数据来实现吗? 答: 1. nodejs的 http://socket.io 支持上述 李宏训 所说的三种方式,另外还支持 Flash Soc ...
- .apache.commons.io 源代码学习(一)
java的初学者,准备通读各种高水平源代码,提升能力. 为了避免自己的惰性,写博客. 版本:2.5 开发平台:netbeans. 今天是第一天,网上先看个例子:http://www.importnew ...
- [BZOJ3211]花神游历各国&&[BZOJ3038] 上帝造题的七分钟2 树状数组+并查集
3211: 花神游历各国 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 4057 Solved: 1480[Submit][Status][Discu ...
- ubuntu 安装TensorFlow
1.安装pip $ sudo apt-get install python-pip python-dev 2.安装 TensorFlow for Python 2.7 # Ubuntu/Linux - ...
- 元素类型 “meta” 必须由匹配的结束标记 “” 终止
报错 org.xml.sax.SAXParseException: 元素类型 “meta” 必须由匹配的结束标记 “” 终止 系统自动创建 <meta charset="UTF-8&q ...
- cdq分治浅谈
$cdq$分治浅谈 1.分治思想 分治实际上是一种思想,这种思想就是将一个大问题划分成为一些小问题,并且这些小问题与这个大问题在某中意义上是等价的. 2.普通分治与$cdq$分治的区别 普通分治与$c ...