如何使用joda Date Time恢复json String

时间:2015-07-24 16:46:29

标签: java json serialization jodatime

我有Pojo作为

public class Person
{

    private String name;

    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDate dob;

    public Person()
    {
    }

    public Person(String name, LocalDate dob)
    {
        this.name = name;
        this.dob = dob;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public LocalDate getDob()
    {
        return dob;
    }

    public void setDob(LocalDate dob)
    {
        this.dob = dob;
    }
}

当我尝试运行像

这样的代码时
public static void main(String[] args) throws IOException{

        Person person = new Person("Bob",new LocalDate(1900,02,22));
        Gson gson = new Gson();
        String json = gson.toJson(person);
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JodaModule());
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);


        Person returnPojo = mapper.readValue(json, Person.class);

        System.out.print(returnPojo);
}

我一直以

为例
  

线程“main”中的异常com.fasterxml.jackson.databind.JsonMappingException:意外的令牌(START_OBJECT),预期的START_ARRAY:预期的JSON数组,字符串或数字    at [来源:{“name”:“Bob”,“dob”:{“iLocalMillis”: - 2204496000000,“iChronology”:{“iBase”:{“iMinDaysInFirstWeek”:4}}}}; line:1,column:14](通过引用链:pojo.Person [“dob”])

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你正在和Gson一起写JSON(它不了解@JsonSerialize)。由于日期格式不正确,杰克逊未能阅读日期。

尝试这样的事情:

tasks-splash-content