Java 解析日期Gson:NumberFormatException
我对java和Gson相当陌生,我一直在尝试解析nobelprize.org api中的一些数据。我能够解析一些信息,但当我包含日期时,我似乎总是出错。我将如何着手修复此错误 我尝试了.setDateFormatyyyy-MM-dd,但仍然出现相同的错误Java 解析日期Gson:NumberFormatException,java,json,gson,Java,Json,Gson,我对java和Gson相当陌生,我一直在尝试解析nobelprize.org api中的一些数据。我能够解析一些信息,但当我包含日期时,我似乎总是出错。我将如何着手修复此错误 我尝试了.setDateFormatyyyy-MM-dd,但仍然出现相同的错误 Gson mainparser = new GsonBuilder() .setDateFormat("yyyy-MM-dd") .create(); mainparser.fromJson(line, ParsedJson.class);
Gson mainparser = new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();
mainparser.fromJson(line, ParsedJson.class);
*编辑:示例Json
"laureates": [
{
"id": "1",
"firstname": "Wilhelm Conrad",
"surname": "Röntgen",
"born": "1845-03-27",
"died": "1923-02-10",
"bornCountry": "Prussia (now Germany)",
"bornCountryCode": "DE",
"bornCity": "Lennep (now Remscheid)",
"diedCountry": "Germany",
"diedCountryCode": "DE",
"diedCity": "Munich",
"gender": "male",
"prizes": [
{
"year": "1901",
"category": "physics",
"share": "1",
"motivation": "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\"",
"affiliations": [
{
"name": "Munich University",
"city": "Munich",
"country": "Germany"
}
]
}
]
},
]
您可以尝试对日期属性使用JsonDeserializer
public class DateDeserializer implements JsonDeserializer<Date> {
public DateDeserializer() {
}
@Override
public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
String dateStr = element.getAsString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
return format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
你能把这封信寄出去吗response@Nomanaliabbasi在编辑复选框中添加了此选项。我建议您不要使用日期。那门课设计得很糟糕,而且早已过时。改为使用localdatefrom。您确定id:1有效吗?在您的代码中,它被定义为int。这是一个愚蠢的问题,但如何根据您的答案实现JsonDeserializer类呢。我似乎得到了这样的结论:DateDeserializer不是抽象的,也不会重写抽象方法,而且@override:method也不会重写或实现超类型的方法。我不确定我是否理解您的问题,但是。我想你认为JsonDeserializer是一个自定义类,但它是gson的类。哦,很抱歉,我想说的是如何包含DateDeserializer类,因为目前它似乎没有覆盖JsonDeserializer中的方法,并且给了我上面的非抽象/不覆盖错误。JsonDeserializer不是一个抽象类。这是一个接口。你不应该有任何错误。我不知道你为什么会犯这样的错误。您只需要创建一个java文件并复制粘贴上面的DateDeserializer类。
java.lang.reflect.InvocationTargetException
Caused by: com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "1845-03-27"
Caused by: java.lang.NumberFormatException: For input string: "1845-03-27"
"laureates": [
{
"id": "1",
"firstname": "Wilhelm Conrad",
"surname": "Röntgen",
"born": "1845-03-27",
"died": "1923-02-10",
"bornCountry": "Prussia (now Germany)",
"bornCountryCode": "DE",
"bornCity": "Lennep (now Remscheid)",
"diedCountry": "Germany",
"diedCountryCode": "DE",
"diedCity": "Munich",
"gender": "male",
"prizes": [
{
"year": "1901",
"category": "physics",
"share": "1",
"motivation": "\"in recognition of the extraordinary services he has rendered by the discovery of the remarkable rays subsequently named after him\"",
"affiliations": [
{
"name": "Munich University",
"city": "Munich",
"country": "Germany"
}
]
}
]
},
]
public class DateDeserializer implements JsonDeserializer<Date> {
public DateDeserializer() {
}
@Override
public Date deserialize(JsonElement element, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
String dateStr = element.getAsString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
return format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
public class Laureates {
int id;
String firstname;
String surname;
@JsonAdapter(DateDeserializer.class)
Date born;
@JsonAdapter(DateDeserializer.class)
Date died;
String bornCountry;
String bornCountryCode;
String bornCity;
String diedCountry;
String diedCountryCode;
String diedCity;
String gender;
List<Prizes> prizes;
...Getters/Setters
}