Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Gson反序列化Java 8 LocalDateTime_Java_Json_Java 8_Gson - Fatal编程技术网

使用Gson反序列化Java 8 LocalDateTime

使用Gson反序列化Java 8 LocalDateTime,java,json,java-8,gson,Java,Json,Java 8,Gson,我有一个格式为“2014-03-10T18:46:40.000Z”的带有日期时间属性的JSON,我想使用Gson将其反序列化为java.time.LocalDateTime字段 当我尝试反序列化时,出现以下错误: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING 反序列化LocalDateTime属性时会发生错误,因为GSON无法解析属性值,因为它不知道LocalDateTime对象 使用GsonBuil

我有一个格式为“2014-03-10T18:46:40.000Z”的带有日期时间属性的JSON,我想使用Gson将其反序列化为java.time.LocalDateTime字段

当我尝试反序列化时,出现以下错误:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

反序列化LocalDateTime属性时会发生错误,因为GSON无法解析属性值,因为它不知道LocalDateTime对象

使用GsonBuilder的registerTypeAdapter方法定义自定义LocalDateTime适配器。 下面的代码片段将帮助您反序列化LocalDateTime属性

Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
    @Override
    public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        Instant instant = Instant.ofEpochMilli(json.getAsJsonPrimitive().getAsLong());
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }
}).create();
Gson Gson=new GsonBuilder().registerTypeAdapter(LocalDateTime.class,new JsonDeserializer()){ @凌驾 公共LocalDateTime反序列化(JsonElement json,类型,JsonDeserializationContext JsonDeserializationContext)引发JsonParseException{ Instant Instant=Instant.ofEpochMilli(json.getAsJsonPrimitive().getAsLong()); 返回LocalDateTime.ofInstant(instant,ZoneId.systemDefault()); } }).create();
要扩展@Randula的答案,将分区日期时间字符串(2014-03-10T18:46:40.000Z)解析为JSON:

Gson Gson=new GsonBuilder().registerTypeAdapter(LocalDateTime.class,new JsonDeserializer()){ @凌驾 公共LocalDateTime反序列化(JsonElement json,类型,JsonDeserializationContext JsonDeserializationContext)引发JsonParseException{ 返回ZonedDateTime.parse(json.getAsJsonPrimitive().getAsString()).toLocalDateTime(); } }).create();
进一步扩展@Evers答案:

您可以使用lambda进一步简化,如下所示:

GSON GSON = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, jsonDeserializationContext) ->
    ZonedDateTime.parse(json.getAsJsonPrimitive().getAsString()).toLocalDateTime()).create();
GSON GSON=new GsonBuilder().registerTypeAdapter(LocalDateTime.class,(JsonDeserializer)(json,type,jsonDeserializationContext)->
parse(json.getAsJsonPrimitive().getAsString()).toLocalDateTime()).create();

以下内容对我很有用

Java:

Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() { 
@Override 
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 

return LocalDateTime.parse(json.getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); } 

}).create();

Test test = gson.fromJson(stringJson, Test.class);
Gson Gson=new GsonBuilder().registerTypeAdapter(LocalDateTime.class,new JsonDeserializer(){ @凌驾 公共LocalDateTime反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext)抛出JsonParseException{ 返回LocalDateTime.parse(json.getAsString(),DateTimeFormatter.of模式(“yyyy-MM-dd-HH:MM”);} }).create(); Test=gson.fromJson(stringJson,Test.class); 其中stringJson是存储为字符串类型的Json

Json:

Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() { 
@Override 
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { 

return LocalDateTime.parse(json.getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); } 

}).create();

Test test = gson.fromJson(stringJson, Test.class);
“日期字段”:“2020-01-30 15:00”


其中,dateField是stringJson字符串变量中存在的LocalDateTime类型。

这是在序列化或反序列化期间发生的。如果是第二个-检查json是否有效-例如,它必须以
{
开头(又称BEGIN\u对象),但您的json以字符开头,在反序列化过程中出错。json->Object。但我认为我的json是可以的。如果我在我的对象中用字符串替换我的属性LocalDateTime,它就可以工作。json的格式为
{“key”:“value”}
您的json绝对无效。正如我在gson
{
BEGIN\u对象
并且它说它丢失了检查json是否正确。你也可以打印
序列化的
对象,看看json应该是什么样子:)好的,我现在明白它为什么不起作用了。但是也许你知道我能做些什么吗?比如说,如果我有一个带有特殊日期和时间的字符串来反序列化这个字段和c创建我需要的对象?在我看到字符串之前,我不能告诉你任何事情。但是,无论哪种方式,你都不能反序列化它,除非它是有效的json。IMHO你能做的最好的(也许是唯一的)事情是使这个字符串成为有效的json。要反序列化类型“type”的参数这到底是什么?@mcgyver5-它是一个
java.lang.reflect.Type
它不是一个分区日期时间字符串,而是一个瞬间()