📜  优点-物件

📅  最后修改于: 2020-11-18 09:05:42             🧑  作者: Mango


ObjectMapper是Boon库的主要actor类。 ObjectMapper类提供用于在基本POJO(普通旧Java对象)之间或从通用JSON树模型(JsonNode)之间读取和写入JSON的功能,以及用于执行转换的相关功能。它也可以高度自定义,以使用不同样式的JSON内容,并支持更高级的Object概念,例如多态性和Object Identity。

以下示例使用ObjectMapper类将JSON字符串解析为Student对象。

import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

public class BoonTester {
   public static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();
      String jsonString = "{\"name\":\"Mahesh\", \"age\":21}";

      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student);
   }
}
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+ " ]";
   }
}

输出

Student [ name: Mahesh, age: 21 ]