gson Java使用本地日期时间解析JSON 0000-00-00 00:00:00

qjp7pelc  于 2022-11-06  发布在  Java
关注(0)|答案(1)|浏览(255)

我遇到了一个问题。我正在调用我的Web服务器,它返回给我一个JSON字符串。在这个JSON字符串中,我有几个如下所示的对象:

public class Transaction {

    private int id;
    private LocalDateTime datetime;
    private double quantity;
    private double avgPrice;

    public ArrayList<Transaction> parseJsonToList(String StrJSON) {

        Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, jsonDeserializationContext) -> {
            try{
                return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            } catch (DateTimeParseException e){
                return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));
            }
        }).create();

        Type listType = new TypeToken<ArrayList<Transaction>>() {}.getType();

        return gson.fromJson(StrJSON, listType);

    }

}

现在,当我调用parseJsonToList()方法时,我创建了一个GsonBuilder,它应该将日期时间从JSON转换为LocalDateTime属性(有或没有微时间)。我现在遇到的问题是,一些日期时间值是0000-00-00 00:00:00。这给我带来了以下错误:

Exception in thread "main" java.time.format.DateTimeParseException: Text '0000-00-00 00:00:00' could not be parsed at index 19
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1953)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:493)
    at com.company.Transaction.lambda$parseJsonToList$1(Transaction.java:109)
    at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:103)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.Gson.fromJson(Gson.java:810)
    at com.google.gson.Gson.fromJson(Gson.java:775)
    at com.google.gson.Gson.fromJson(Gson.java:724)

现在我不能更改JSON,那么有没有办法仍然存储它或使用空值?

cu6pst1q

cu6pst1q1#

您可以使用DateTimeFormatter#withResolverStyle(ResolverStyle.LENIENT)

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ENGLISH)
                .withResolverStyle(ResolverStyle.LENIENT);
        String str = "0000-00-00 00:00:00";

        LocalDateTime ldt = LocalDateTime.parse(str, dtf);
        System.out.println(ldt);
    }
}

输出:

-0001-11-30T00:00

相关问题