1. JSONObject
JSONObject object = new JSONObject(entity);
JSONObject 是无序的
2. Gson
Gson对于时间类型需要额外处理
Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDate.class,new LocalDateAdapter())
.registerTypeAdapter(LocalDateTime.class,new LocalDateTimeAdapter())
.create();
public class LocalDateAdapter implements JsonSerializer<LocalDate> {
@Override
public JsonElement serialize(LocalDate localDate, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
}
public class LocalDateTimeAdapter implements JsonSerializer<LocalDateTime> {
@Override
public JsonElement serialize(LocalDateTime localDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));
}
}