0

我已经基于这个类创建了一个带有杰克逊的 JSON:

    public class One {
    public long param;

    public List<Two> two;

    public static class Two{
        public long param;
        public List<Short> param2;
    }
}

我为每个类定义了两个构造函数,并使用它来创建稍后写入 json 文件的对象。结果是(有效的 JSON):

[{"param":1,"Two":[{"param":4,"param2":[1,2,3]},{"param":5,"parma2":[4,5,6]}]},{"param":2,"Two":[{"param":6,"param2":[1,2,3]}]}]

现在我想读取并加载参数并定义:

    mapper.configure(Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); //tried with and without
    JsonFactory f = new JsonFactory();
    JsonParser jp;
jp = f.createJsonParser(FILE);
    jp.nextToken();
    while (jp.nextToken() == JsonToken.START_OBJECT) {
         mapper.readValue(jp, One.class);
     }
     jp.close();

我有这个错误:

Can not deserialize instance of json.One$Two out of START_ARRAY
4

1 回答 1

1

我找到了解决方案。

json文件的加载:

List<One> myObjects = new ArrayList<One>();
myObjects = mapper.readValue(FILE, mapper.getTypeFactory().constructCollectionType(List.class, One.class));

类中的代码仅通过添加来编辑@JsonProperty("Two")

   public class One {
    public long param;
    @JsonProperty("Two")
    public List<Two> two;

    public static class Two{
        public long param;
        public List<Short> param2;
    }
}
于 2013-08-28T11:41:49.490 回答