程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java的Jackson庫的應用及其樹范型的入門進修教程

Java的Jackson庫的應用及其樹范型的入門進修教程

編輯:關於JAVA

Java的Jackson庫的應用及其樹范型的入門進修教程。本站提示廣大學習愛好者:(Java的Jackson庫的應用及其樹范型的入門進修教程)文章只能為提供參考,不一定能成為您想要的結果。以下是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 ]

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved