我正在使用此方法将任何对象转换为 json 字符串:
private String objectToJson(Object object) throws IOException {
// write JSON
StringWriter writer = new StringWriter();
ObjectMapper mapper = new ObjectMapper();
final JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(writer);
jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());
mapper.writeValue(jsonGenerator, object);
return writer.toString();
}
当正在打印的对象包含 java.util.Date 或 jodatime 的 DateTime 字段时,打印的值是自纪元以来的毫秒数。相反,我想以标准的“HH:MM:SS”表示法漂亮地打印它们。我该怎么做呢?