Reputation: 421
I have create a JSON with Jackson based on this class:
public class One {
public long param;
public List<Two> two;
public static class Two{
public long param;
public List<Short> param2;
}
}
I have defined two constructor for each class and I use it for the creation of the objects that later are written on the json file. The outcome is (valid 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]}]}]
Now I would like to read and load the parameters and I define:
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();
I have this error:
Can not deserialize instance of json.One$Two out of START_ARRAY
Upvotes: 0
Views: 1834
Reputation: 421
I have found the solution.
Load of the json file:
List<One> myObjects = new ArrayList<One>();
myObjects = mapper.readValue(FILE, mapper.getTypeFactory().constructCollectionType(List.class, One.class));
The code in the class is edited by adding only @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;
}
}
Upvotes: 1