0

I'm fairly new to java and Gson and I've been trying to parse some date from the nobelprize.org api. I've been able to parse through some of the info but when I include dates I seem to always hit errors. How would i go about fixing this error?

I've tried .setDateFormat("yyyy-MM-dd") but I still get the same error.

Gson mainparser = new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();

mainparser.fromJson(line, ParsedJson.class);
public class ParsedJson {

    List<Laureates> laureates;

    public List<Laureates> getLaureates() {
        return laureates;
    }

    public void setLaureates(List<Laureates> laureates) {
        this.laureates = laureates;
    }


}
public class Laureates {
    int id;
    String firstname;
    String surname;
    Date born;
    Date died;
    String bornCountry;
    String bornCountryCode;
    String bornCity;
    String diedCountry;
    String diedCountryCode;
    String diedCity;
    String gender;
    List<Prizes> prizes;
...Getters/Setters

}

This is the error I get:

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"

*Edit: Example 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"
                        }
                    ]
                }
            ]
        },
]
pankaj sharma
  • 181
  • 2
  • 12
YamYam
  • 15
  • 5

1 Answers1

1

You can try to use JsonDeserializer for Date attributes.

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;
    }
}

in your model class add JsonAdapter for attributes

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

}
Ergin Ersoy
  • 890
  • 8
  • 28
  • Dumb question but how do I implement the JsonDeserializer class from your answer. As I seem to either get this: DateDeserializer is not abstract and does not override abstract method, And also this from the @Override: Method does not override or implement a method from a supertype – YamYam Apr 10 '19 at 07:41
  • I am not sure if i understand your question but. I think you thought `JsonDeserializer` is a custom class but it is `gson`'s class. – Ergin Ersoy Apr 10 '19 at 07:44
  • Oh sorry I meant to say how would I include DateDeserializer class, as currently it does not seem to override the method from JsonDeserializer and is giving me the not abstract/does not override errors from above – YamYam Apr 10 '19 at 07:50
  • `JsonDeserializer` is not an abstract class. It is an interface. You shouldn't get any errors. I am not sure why you are getting that error. You just needed to create a java file and copy paste the `DateDeserializer` class above. – Ergin Ersoy Apr 10 '19 at 07:59