Boon - 来自对象


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

例子

以下示例使用 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;
   }
}

输出

这会产生以下输出 -

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