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



