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编译如下类:
Student [ name: Mahesh, age: 21 ] { "name" : "Mahesh", "age" : 21 }
步骤 - 需要记住
以下是这里要考虑的重要步骤。
第1步:创建ObjectMapper对象。
创建ObjectMapper对象。它是一个可重复使用的对象。\
ObjectMapper mapper = new ObjectMapper();
//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"); Iteratoriterator = 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编译如下类:
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"); Iteratoriterator = 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编译如下类:
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编译如下类:
Name: Mahesh Kumar Age: 21 Verified: No Marks: [ 100 90 85 ]