使用GSON转换为Instant的字段为“ yyyy-MM-ddThh:mm:ss”的json,但得到java.lang.NumberFormatException:对于输入字符串:“ 1999-08-24T00:00:00”

问题描述 投票:0回答:1

我正在尝试在小型Java应用程序中与gson进行json解析。我有一个来自.Net业务层的json字符串,其字段为“ 1999-08-24T00:00:00”。在像用户模型这样的模型中,我有java.time.Instant birthDay字段。与gson我试图将json字符串添加到我的用户模型。我也有一个InstantDeserializer类。但是,当我尝试将其转换时,却收到类似java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)..]]的消息。

在使用即时类型之前,我使用的是Date类。我写了DateDeserializer类,但我知道不赞成使用Date类。我用谷歌搜索了很多页面。我尝试了很多事情,但我没有弄清楚。所以我只想问我在哪里犯错。我该怎么办?如何使我的代码更清晰或最好的方法是什么?如果您可以提供一些代码示例,我会更好地理解。

任何建议,我们都感激..

这是我的代码。

JSON字符串:

{
   "Value":{
      "ID":"123",
      "NAME":"John",
      "SURNAME":"Concept",
      "BIRTHDAY":"1999-08-24T00:00:00",
      "PAYMENTINFORMATION":[
         {
            "ID":"1",
            "PAYMENTINFO":"RECIEVED"
         }
      ]
   },
   "Succued": true
}

UserModel类

] >>
package Models;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;
import java.util.Date;
import com.google.gson.annotations.SerializedName;
import java.time.Instant;

public class UserModel {

    private long id;
    private String name;
    private String surname;
    private Instant birthday;
    private List<PaymentModel> paymentInformation;

    //GETTER SETTER

    public UserModel() {
        paymentInformation= new ArrayList<>();
    }
}

InstantDeserializer类

package Util;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class InstantDeSerializer implements JsonDeserializer<Instant> {

    @Override
    public Instant deserialize(JsonElement jelement, Type type, JsonDeserializationContext jdc) throws JsonParseException {
        Instant insObj= Instant.ofEpochMilli(jelement.getAsJsonPrimitive().getAsLong());
        return insObj;
    }

}

和Main class

]
public class JSONTryMe {
    public static void main(String[] args) throws Exception {

        JSONObject responseJSON = new JSONObject(jsonString);
        if (responseJSON.isNull("Value")) {
            return;
        }

        GsonBuilder build = new GsonBuilder();
        build.registerTypeAdapter(Instant.class, new InstantDeSerializer());
        Gson gObj = build.create();
        UserModel user = gObj.fromJson(responseJSON.getJSONObject("Value").toString(), UserModel.class);

        System.out.println(user.getBirthday().toString());    
    }
}

请注意错误stackTrace为

java.lang.NumberFormatException: For input string: "1999-08-24T00:00:00"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:589)
    at java.lang.Long.parseLong(Long.java:631)
    at com.google.gson.JsonPrimitive.getAsLong(JsonPrimitive.java:206)
    at Util.InstantDeSerializer.deserialize(InstantDeSerializer.java:25)
    at Util.InstantDeSerializer.deserialize(InstantDeSerializer.java:21)
    at com.google.gson.internal.bind.TreeTypeAdapter.read(TreeTypeAdapter.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
    at com.google.gson.Gson.fromJson(Gson.java:932)
    at com.google.gson.Gson.fromJson(Gson.java:897)
    at com.google.gson.Gson.fromJson(Gson.java:846)
    at com.google.gson.Gson.fromJson(Gson.java:817)
    at Source.JSONTryMe.main(JSONTryMe.java:85)

我正在尝试在小型Java应用程序中与gson进行json解析。我有一个来自.Net业务层的json字符串,其字段为“ 1999-08-24T00:00:00”。在我的模型(如用户模型)中,我有...

json gson deserialization numberformatexception java.time.instant
1个回答
1
投票

这里有几个概念上的缺陷:

  • 出生日期应使用LocalDate
© www.soinside.com 2019 - 2024. All rights reserved.