Java的Jackson库的使用及其树模型的入门学习教程
jackson第一个程序
在进入学习jackson库的细节之前,让我们来看看应用程序操作功能。在这个例子中,我们创建一个student类。将创建一个json字符串学生的详细信息,并将其反序列化到学生的对象,然后将其序列化到json字符串。
创建一个名为jacksontester在java类文件 c:\>jackson_workspace.
文件: jacksontester.java
import java.io.ioexception; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; import org.codehaus.jackson.map.serializationconfig; public class jacksontester { public static void main(string args[]){ objectmapper mapper = new objectmapper(); string jsonstring = "{\"name\":\"mahesh\", \"age\":21}"; //map json to student try { student student = mapper.readvalue(jsonstring, student.class); system.out.println(student); mapper.enable(serializationconfig.feature.indent_output); jsonstring = mapper.writevalueasstring(student); system.out.println(jsonstring); } 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
验证输出
student [ name: mahesh, age: 21 ] { "name" : "mahesh", "age" : 21 }
步骤 - 需要记住
以下是这里要考虑的重要步骤。
第1步:创建objectmapper对象。
创建objectmapper对象。它是一个可重复使用的对象。\
objectmapper mapper = new objectmapper();
第2步:反序列化json到对象。
从json对象使用readvalue()方法来获取。通过json字符串和对象类型作为参数json字符串/来源。
//object to json conversion student student = mapper.readvalue(jsonstring, student.class);
第3步:序列化对象到json。
使用writevalueasstring()方法来获取对象的json字符串表示。
//object to json conversion jsonstring = mapper.writevalueasstring(student);
jackson树模型
树模型准备json文件的内存树表示。 objectmapper构建jsonnode节点树。这是最灵活的方法。它类似于dom解析器的xml。
从json创建树
objectmapper提供一个指针树的根节点在读取json之后。根节点可用于遍历完全树。考虑下面的代码片段获得提供json字符串的根节点。
//create an objectmapper instance objectmapper mapper = new objectmapper(); string jsonstring = "{\"name\":\"mahesh kumar\", \"age\":21,\"verified\":false,\"marks\": [100,90,85]}"; //create tree from json jsonnode rootnode = mapper.readtree(jsonstring);
遍历树模型
使用相对路径来根节点在遍历树,并处理该数据得到的每个节点。考虑下面的代码片段遍历提供的根节点的树。
jsonnode namenode = rootnode.path("name"); system.out.println("name: "+ namenode.gettextvalue()); jsonnode marksnode = rootnode.path("marks"); iterator iterator = marksnode.getelements();
示例
创建一个名为jacksontester在java类文件目录 c:\>jackson_workspace.
file: jacksontester.java
import java.io.ioexception; import java.util.iterator; import org.codehaus.jackson.jsonnode; 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(); string jsonstring = "{\"name\":\"mahesh kumar\", \"age\":21,\"verified\":false,\"marks\": [100,90,85]}"; jsonnode rootnode = mapper.readtree(jsonstring); jsonnode namenode = rootnode.path("name"); system.out.println("name: "+ namenode.gettextvalue()); jsonnode agenode = rootnode.path("age"); system.out.println("age: " + agenode.getintvalue()); jsonnode verifiednode = rootnode.path("verified"); system.out.println("verified: " + (verifiednode.getbooleanvalue() ? "yes":"no")); jsonnode marksnode = rootnode.path("marks"); iterator<jsonnode> iterator = marksnode.getelements(); system.out.print("marks: [ "); while (iterator.hasnext()) { jsonnode marks = iterator.next(); system.out.print(marks.getintvalue() + " "); } system.out.println("]"); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
验证输出结果
使用javac编译如下类:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到结果:
c:\jackson_workspace>java jacksontester
验证输出
name: mahesh kumar age: 21 verified: no marks: [ 100 90 85 ]
树到json转换
在这个例子中,我们已经使用jsonnode并将其写入到一个json文件,并读回创建了一棵树。
创建一个名为jacksontester在java类文件目录 c:\>jackson_workspace.
file: jacksontester.java
import java.io.file; import java.io.ioexception; import java.util.iterator; import org.codehaus.jackson.jsonnode; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; import org.codehaus.jackson.node.arraynode; import org.codehaus.jackson.node.objectnode; public class jacksontester { public static void main(string args[]){ jacksontester tester = new jacksontester(); try { objectmapper mapper = new objectmapper(); jsonnode rootnode = mapper.createobjectnode(); jsonnode marksnode = mapper.createarraynode(); ((arraynode)marksnode).add(100); ((arraynode)marksnode).add(90); ((arraynode)marksnode).add(85); ((objectnode) rootnode).put("name", "mahesh kumar"); ((objectnode) rootnode).put("age", 21); ((objectnode) rootnode).put("verified", false); ((objectnode) rootnode).put("marks",marksnode); mapper.writevalue(new file("student.json"), rootnode); rootnode = mapper.readtree(new file("student.json")); jsonnode namenode = rootnode.path("name"); system.out.println("name: "+ namenode.gettextvalue()); jsonnode agenode = rootnode.path("age"); system.out.println("age: " + agenode.getintvalue()); jsonnode verifiednode = rootnode.path("verified"); system.out.println("verified: " + (verifiednode.getbooleanvalue() ? "yes":"no")); jsonnode marksnode1 = rootnode.path("marks"); iterator<jsonnode> iterator = marksnode1.getelements(); system.out.print("marks: [ "); while (iterator.hasnext()) { jsonnode marks = iterator.next(); system.out.print(marks.getintvalue() + " "); } system.out.println("]"); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
验证结果
使用javac编译如下类:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到的结果:
c:\jackson_workspace>java jacksontester
验证输出
name: mahesh kumar
age: 21
verified: no
marks: [ 100 90 85 ]
从树到java对象转换
在这个例子中,我们已经使用jsonnode并将其写入到一个json文件,并回读然后将一个student对象其转换为创建了一棵树。
创建一个名为jacksontester在java类文件目录 c:\>jackson_workspace.
file: jacksontester.java
import java.io.file; import java.io.ioexception; import java.util.arrays; import org.codehaus.jackson.jsonnode; import org.codehaus.jackson.jsonparseexception; import org.codehaus.jackson.map.jsonmappingexception; import org.codehaus.jackson.map.objectmapper; import org.codehaus.jackson.node.arraynode; import org.codehaus.jackson.node.objectnode; public class jacksontester { public static void main(string args[]){ jacksontester tester = new jacksontester(); try { objectmapper mapper = new objectmapper(); jsonnode rootnode = mapper.createobjectnode(); jsonnode marksnode = mapper.createarraynode(); ((arraynode)marksnode).add(100); ((arraynode)marksnode).add(90); ((arraynode)marksnode).add(85); ((objectnode) rootnode).put("name", "mahesh kumar"); ((objectnode) rootnode).put("age", 21); ((objectnode) rootnode).put("verified", false); ((objectnode) rootnode).put("marks",marksnode); mapper.writevalue(new file("student.json"), rootnode); rootnode = mapper.readtree(new file("student.json")); student student = mapper.treetovalue(rootnode, student.class); system.out.println("name: "+ student.getname()); system.out.println("age: " + student.getage()); system.out.println("verified: " + (student.isverified() ? "yes":"no")); system.out.println("marks: "+arrays.tostring(student.getmarks())); } catch (jsonparseexception e) { e.printstacktrace(); } catch (jsonmappingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } } class student { string name; int age; boolean verified; int[] marks; 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 boolean isverified() { return verified; } public void setverified(boolean verified) { this.verified = verified; } public int[] getmarks() { return marks; } public void setmarks(int[] marks) { this.marks = marks; } }
验证结果
使用javac编译如下类:
c:\jackson_workspace>javac jacksontester.java
现在运行jacksontester看到结果:
c:\jackson_workspace>java jacksontester
验证输出
name: mahesh kumar age: 21 verified: no marks: [ 100 90 85 ]
下一篇: Hadoop集群搭建