Jackson ObjectMapper用数组反序列化对象

时间:2016-11-25 09:44:00

标签: java arrays json jackson objectmapper

有一些类似的问题herehere,但它们与我的情况不太匹配。我的JSON字符串由一个带有数组的对象组成:

{
  "data": [
    {
      "id": 1,
      "title": "Sample training",
      "date": "2016-10-03 10:00:00",
      "subscription": "2016-09-20 12:34:50"
    },
    {
      "id": 2,
      "title": "Second training",
      "date": "2016-10-06 10:00:00",
      "subscription": "2016-09-20 12:54:50"
    }
  ]
}

数组中的每个对象都是Java bean:

public class TrainingInfo {

    private int id;
    private String title;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime date;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime subscription;

    // Consructors, getters and setters omitted 
    // ...
}

我可以阅读单个TrainingInfo

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
TrainingInfo training = mapper.readValue(jsonTrainingInfo, TrainingInfo.class);

但我无法读取整个阵列。我试图创建一个新的Java bean,只是包含一个数组并读取它,如下所示:

private class TrainingsArray {
    private TrainingInfo[] data;

    public TrainingInfo[] getData() {
        return data;
    }

    public void setData(TrainingInfo[] data) {
        this.data = data;
    }
}

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
TrainingsArray trainings = mapper.readValue(jsonTrainingsArray, TrainingsArray.class);

但这只会引发IOException。 我错过了什么?

更新:抛出的异常:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of JsonMapperTest$TrainingsArray: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: {
  "data": [
    {
      "id": 1,
      "title": "Sample training",
      "date": "2016-10-03 10:00:00",
      "subscription": "2016-09-20 12:34:50"
    },
    {
      "id": 2,
      "title": "Second training",
      "date": "2016-10-06 10:00:00",
      "subscription": "2016-09-20 12:54:50"
    }
  ]
}; line: 2, column: 3]

    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
    at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1205)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
    at JsonMapperTest.shouldMapJsonResponseToTrainingsArray(JsonMapperTest.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

1 个答案:

答案 0 :(得分:1)

但这可能是愚蠢的,解决方法是让课程TrainingsArray公开。