📜  皇家宝藏-迄今为止

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


ObjectMapper类可用于在JSON中使用不同的日期格式。它可以用于解析/生成日期的长版本。

下面的示例使用ObjectMapper类从长版本生成Date字符串。

import java.util.Date;
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, \"dateOfBirth\":976559400000}";
      
      //mapper converts long to date automatically
      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student.dateOfBirth);    
      
      //by default mapper converts date to long
      jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   public String name;
   public int age;
   public Date dateOfBirth;
   
   public Student(String name, int age, Date dateOfBirth) {
      this.name = name;
      this.age = age;
      this.dateOfBirth = dateOfBirth;
   }
}

输出

Tue Dec 12 00:00:00 IST 2000
{"name":"Mahesh","age":21,"dateOfBirth":976559400000}