📜  Boon-来自对象

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


ObjectMapper类可用于从Object生成json字符串。

以下示例使用ObjectMapper类从学生对象生成JSON字符串。

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

public class BoonTester {
   public static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();      
      Student student = new Student("Mahesh", 21);
      String jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);
   }
}
class Student {
   public String name;
   public int age;
   public Student(String name, int age) {
      this.name = name;
      this.age = age;
   }
}

输出

{"name":"Mahesh","age":21}