user3369592 user3369592 - 1 month ago 11
Java Question

Can not deserialize instance of parseJason when using Jackson to parse the Json File

I have a Json file :

[
{
"name":"Move",
"$$hashKey":"object:79",
"time":11.32818,
"endTime":18.615535
},
{
"name":"First Red Flash",
"$$hashKey":"object:77",
"time":15.749153
},
{
"name":"Pills",
"subEventTypes":[
"pull down bottle",
"unscrew lid",
"dump pills out",
"screw lid on",
"put bottle away"
],
"$$hashKey":"object:82",
"time":25.130175,
"subEventSplits":[
26.092057,
27.425881,
31.841594,
34.268093
],
"endTime":36.234827
}


]


I tried to parse this Json file using the Jackson.
I wrote the following code:

public class Holder{
public Holder(){};
//getter and setters

String name;
List<String> subEventTypes = new ArrayList<>();
Double time;
String $$hashKey;
Double endTime;
List<Double> subEventSplits = new ArrayList<>();

}
class MapperClass{
List<Holder> list = new ArrayList<>();
}

public static void main(String[] args) throws JsonProcessingException, IOException
{
ObjectMapper mapper = new ObjectMapper();
List<Holder> list = mapper.readValue(new File("data.json"), mapper.getTypeFactory().constructCollectionType(
List.class, Holder.class));

}


When I run the program, it showed this error : "

No suitable constructor found for type [simple type, class parseJason$Holder]: can not instantiate from JSON object (need to add/enable type information?)


".
Is there anything wrong with my code? or I have to use another way to parse my Json file.

Answer

try

list = mapper.readValue(
                jsonString,
                objectMapper.getTypeFactory().constructCollectionType(
                        List.class, Holder.class));