Boon 简明教程

Boon - From Object

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

Example

以下示例使用 ObjectMapper 类从 Student 对象生成 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;
   }
}

Output

生成以下输出:

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