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}
廣告
© . All rights reserved.