JSON

Java的Jackson库的使用及其树模型的入门学习教程(2)

字号+ 作者:H5之家 来源:H5之家 2016-01-17 13:46 我要评论( )

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.JsonMappin

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 ]

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JavaScript转换与解析JSON方法实例详解第1/2页

    JavaScript转换与解析JSON方法实例详解第1/2页

    2016-02-10 21:25

  • 未来编程的9大猜想:JavaScript不必亲自编写

    未来编程的9大猜想:JavaScript不必亲自编写

    2016-01-30 13:01

  • Objective-C与JavaScript交互的那些事

    Objective-C与JavaScript交互的那些事

    2016-01-30 13:00

  • 常见的java代码转换成json

    常见的java代码转换成json

    2016-01-27 17:01

网友点评
-