当我尝试将隐藏的json与null模型使用GSON时出错

时间:2016-02-03 05:45:17

标签: java json gson

我有String witj Json:

String str = "[{
    "serviceid": "312",
    "servicetype": "0",
    "nextrecurringchargedate": "null"
},
{
    "serviceid": "104",
    "servicetype": "0",
    "nextrecurringchargedate": "null"
},
{
    "serviceid": "310",
    "servicetype": "0",
    "nextrecurringchargedate": "null"
}]";

我有模特:

public class NextChDay {
    @SerializedName("serviceid")
    private Long serviceId;
    @SerializedName("servicetype")
    private Long serviceType;
    @SerializedName("nextrecurringchargedate")
    private Date nextrecurringchargedate;

    public NextChDay() {
    }

    public NextChDay(Long serviceId, Long serviceType, Date nextrecurringchargedate) {
        this.serviceId = serviceId;
        this.serviceType = serviceType;
        this.nextrecurringchargedate = nextrecurringchargedate;
    }

// getters and setters

我希望将Json转换为模型使用GSON库

NextChDay[] nextChDays = GSON.fromJson(str, NextChDay[].class);

但我有一个错误:

java.text.ParseException: Unparseable date: "null"

以前,我对“null”没有任何问题,我不明白如何解决这个问题。请帮帮我。

1 个答案:

答案 0 :(得分:1)

null是JSON中的保留字/值,因此不应该用引号或双引号括起来。当它被包装时,它实际上被认为是包含字符null的字符串值。

所以,试着替换这一行:

"nextrecurringchargedate": "null"

这一行:

"nextrecurringchargedate": null