java - 使用 Gson 反序列化 LocalDateTime 不起作用

标签 java json serialization gson

我需要使用 Gson 将字符串转换为对象:

gson.fromJson("{\"message\":\"any msg.\",\"individual\":{\"id\":100,\"citizenshipList\":[{\"date\":[2018,10,15,16,29,36,402000000]}]}}", Response.class)

哪里

public class Response {
    private String message;
    private Individual individual;
}

public class Individual {
 private Integer id;
 private List<Citizenship> citizenshipList = new ArrayList<>();
}

public class Citizenship {

  @DateTimeFormat(pattern="d::MMM::uuuu HH::mm::ss")
  LocalDateTime date;

}

我收到此错误

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 122 path $.individual.citizenshipList[0].date

我也尝试过修改后的 Gson :

Gson gson1 = new GsonBuilder()
            .registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
                @Override
                public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                    JsonObject jo = json.getAsJsonObject();
                    return LocalDateTime.of(jo.get("year").getAsInt(),
                            jo.get("monthValue").getAsInt(),
                            jo.get("dayOfMonth").getAsInt(),
                            jo.get("hour").getAsInt(),
                            jo.get("minute").getAsInt(),
                            jo.get("second").getAsInt(),
                            jo.get("nano").getAsInt());
                }
            }).create();

但这给了我这个错误:

java.lang.IllegalStateException: Not a JSON Object: [2018,10,15,16,29,36,402000000]

最佳答案

您发布的两个错误说明了问题所在:

Not a JSON Object: [2018,10,15,16,29,36,402000000]

Expected BEGIN_OBJECT but was BEGIN_ARRAY

[2018,10,15,16,29,36,402000000] 是一个 JSON 数组,GSON 需要一个 JSON 对象,例如:{}

解决此问题的一种方法是修改您的 JsonDeserializer 以使用 JsonArray 而不是 JsonObject:

Gson gson1 = new GsonBuilder()
            .registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
                @Override
                public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                    JsonArray array = json.getJSONArray("date");
                    return LocalDateTime.of(
                                            // Set all values here from
                                            // `array` variable
                                            );
                }
            }).create();

关于java - 使用 Gson 反序列化 LocalDateTime 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52824497/

相关文章:

c - 在 C 中通过网络序列化和同步数据结构

.net - 将对象反序列化为现已签名和版本控制的程序集

c# - JSON.NET 序列化在 ApiController 中的行为不同

java - 如何控制加密字符串的长度?

java - Android给ImageView添加边框图片

php - 如何在 PHP 中访问 JSON 解码数组

ruby-on-rails - Rails 通过关联渲染 has_many 的 JSON

java - 以这种方式处理异常以在用户名重复时返回消息

java - HttpURLConnection 不释放资源

javascript - 如何在jquery脚本中调用Node.js服务?