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 ]