- Boon 教程
- Boon - 主頁
- Boon - 概覽
- Boon - 環境設定
- 解析 JSON
- Boon - 轉換為物件
- Boon - 轉換為對映
- Boon - 原始碼
- 生成 JSON
- Boon - 從物件
- Boon - 從對映
- 日期處理
- Boon - Long 轉化日期
- Boon - String 轉化日期
- Boon - 生成日期
- 註解
- Boon - @JsonIgnore
- Boon - @JsonInclude
- Boon - @JsonViews
- Boon - @JsonProperty
- Boon 實用資源
- Boon - 快速指南
- Boon - 實用資源
- Boon - 討論
Boon - 生成日期
ObjectMapper 類可用於處理 JSON 中的不同日期格式。它還可用於生成日期物件。預設情況下,ObjectMapper 以長整型毫秒版本生成日期。如果使用 JsonFactory.createUseJSONDates() 方法返回的 ObjectMapper,我們可以在解析期間獲取日期的字串版本。
示例
以下示例使用 ObjectMapper 類透過解析 JSON 生成日期字串。
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.createUseJSONDates();
String jsonString = "{\"name\":\"Mahesh\", \"age\":21, \"dateOfBirth\":\"1998-08-11T11:31:00.034Z\" }";
//mapper converts String to date automatically
Student student = mapper.readValue(jsonString, Student.class);
System.out.println(student.dateOfBirth);
//Mapper converts date to date string now
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 Aug 11 17:01:00 IST 1998
{"name":"Mahesh","age":21,"dateOfBirth":"1998-08-11T11:31:00.034Z"}
廣告