实例解析Java的Jackson库中的数据绑定
程序员文章站
2024-03-08 21:19:28
数据绑定api用于json转换和使用属性访问或使用注解pojo(普通java对象)。以下是它的两个类型。
简单数据绑定 - 转换json,从java maps, list...
数据绑定api用于json转换和使用属性访问或使用注解pojo(普通java对象)。以下是它的两个类型。
简单数据绑定 - 转换json,从java maps, lists, strings, numbers, booleans 和 null 对象。
完整数据绑定 - 转换json到任何java类型。我们将在下一章分别绑定。
objectmapper读/写json两种类型的数据绑定。数据绑定是最方便的方式是类似xml的jaxb解析器。
简单的数据绑定
简单的数据绑定是指json映射到java核心数据类型。下表列出了json类型和java类型之间的关系。
让我们来看看简单的数据操作绑定。在这里,我们将映射java基本类型直接json,反之亦然。
创建一个名为jacksontester在java类文件在目录 c:\>jackson_workspace.
file: jacksontester.java
import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; import org.codehaus.jackson.jsongenerationexception; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; public class jacksontester { public static void main(string args[]){ jacksontester tester = new jacksontester(); try { objectmapper mapper = new objectmapper(); map<string,object> studentdatamap = new hashmap<string,object>(); int[] marks = {1,2,3}; student student = new student(); student.setage(10); student.setname("mahesh"); // java object studentdatamap.put("student", student); // java string studentdatamap.put("name", "mahesh kumar"); // java boolean studentdatamap.put("verified", boolean.false); // array studentdatamap.put("marks", marks); mapper.writevalue(new file("student.json"), studentdatamap); //result student.json //{ // "student":{"name":"mahesh","age":10}, // "marks":[1,2,3], // "verified":false, // "name":"mahesh kumar" //} studentdatamap = mapper.readvalue(new file("student.json"), map.class); system.out.println(studentdatamap.get("student")); system.out.println(studentdatamap.get("name")); system.out.println(studentdatamap.get("verified")); system.out.println(studentdatamap.get("marks")); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } } class student { private string name; private int age; public student(){} 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 string tostring(){ return "student [ name: "+name+", age: "+ age+ " ]"; } }
验证结果
使用javac编译如下类:
复制代码 代码如下:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到的结果:
复制代码 代码如下:
c:\jackson_workspace>java jacksontester
验证输出结果
{name=mahesh, age=10} mahesh kumar false [1, 2, 3]
全数据绑定
完全数据绑定是指json映射到任何java对象。
//create an objectmapper instance objectmapper mapper = new objectmapper(); //map json content to student object student student = mapper.readvalue(new file("student.json"), student.class); //map student object to json content mapper.writevalue(new file("student.json"), student);
让我们来看看简单的数据操作绑定。在这里,我们将直接映射java对象到json,反之亦然。
创建一个名为jacksontester在java类文件目录 c:\>jackson_workspace.
file: jacksontester.java
import java.io.file; import java.io.ioexception; import org.codehaus.jackson.jsongenerationexception; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; public class jacksontester { public static void main(string args[]){ jacksontester tester = new jacksontester(); try { student student = new student(); student.setage(10); student.setname("mahesh"); tester.writejson(student); student student1 = tester.readjson(); system.out.println(student1); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private void writejson(student student) throws jsongenerationexception, jsonmappingexception, ioexception{ objectmapper mapper = new objectmapper(); mapper.writevalue(new file("student.json"), student); } private student readjson() throws jsonparseexception, jsonmappingexception, ioexception{ objectmapper mapper = new objectmapper(); student student = mapper.readvalue(new file("student.json"), student.class); return student; } } class student { private string name; private int age; public student(){} 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 string tostring(){ return "student [ name: "+name+", age: "+ age+ " ]"; } }
验证结果
使用javac编译如下类:
复制代码 代码如下:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到的结果:
复制代码 代码如下:
c:\jackson_workspace>java jacksontester
验证输出
复制代码 代码如下:
student [ name: mahesh, age: 10 ]