java - com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of TestAcceptanceCriteria out of START_ARRAY token -
given following json file:
[ {"name":"test_12345_54321", "acceptancecriteria": [ { "given":"i developer", "and":"i have test case write", "when":"i run test", "then":"i report generated details." } ] }, {"name":"test_2", "acceptancecriteria": [ { "given":"i developer test 2", "and":"i have test case write test 2", "when":"i run test test 2", "then":"i report generated details.2" } ] } ]
i trying access values within json file using jackson object mapper. follows:
file jsonfile = new file("results/acceptancecriteria.json"); objectmapper mapper = new objectmapper(); testacceptancecriteria acceptancecriteria = null; acceptancecriteria = mapper.readvalue(jsonfile, testacceptancecriteria.class); system.out.println(acceptancecriteria.getacceptancecriteria());
each time try access variable following error:
com.fasterxml.jackson.databind.jsonmappingexception: can not deserialize instance of file.readers.testacceptancecriteria out of start_array token @ [source: results\acceptancecriteria.json; line: 1, column: 1] @ com.fasterxml.jackson.databind.jsonmappingexception.from(jsonmappingexception.java:270) @ com.fasterxml.jackson.databind.deserializationcontext.reportmappingexception(deserializationcontext.java:1234) @ com.fasterxml.jackson.databind.deserializationcontext.handleunexpectedtoken(deserializationcontext.java:1122) @ com.fasterxml.jackson.databind.deserializationcontext.handleunexpectedtoken(deserializationcontext.java:1075) @ com.fasterxml.jackson.databind.deser.beandeserializerbase.deserializefromarray(beandeserializerbase.java:1374) @ com.fasterxml.jackson.databind.deser.beandeserializer._deserializeother(beandeserializer.java:174) @ com.fasterxml.jackson.databind.deser.beandeserializer.deserialize(beandeserializer.java:150) @ com.fasterxml.jackson.databind.objectmapper._readmapandclose(objectmapper.java:3798) @ com.fasterxml.jackson.databind.objectmapper.readvalue(objectmapper.java:2740) @ file.readers.acceptancecriteriajsonreader.main(acceptancecriteriajsonreader.java:51)
my classes follows:
public class acceptancecriteria{ private string given; private string and; private string when; private string then; public string getgiven(){ return given; } public void setgiven(string given){ this.given = given; } public string getand(){ return and; } public void setand(string and){ this.and = and; } public string getwhen(){ return when; } public void setwhen(string when){ this.when = when; } public string getthen(){ return then; } public void setthen(string then){ this.then = then; } }
...
import java.util.list; public class testacceptancecriteria { private string name; private list<acceptancecriteria> acceptancecriteria; public string getname(){ return name; } public void setname(string name){ this.name = name; } public list<acceptancecriteria> getacceptancecriteria(){ return acceptancecriteria; } public void setacceptancecriteria(list<acceptancecriteria> acceptancecriteria){ this.acceptancecriteria = acceptancecriteria; } }
you trying deserialize array of entities single object, cause problem. let's try:
list<testacceptancecriteria> acceptancecriteria = null; acceptancecriteria = mapper.readvalue(json, new typereference<list<testacceptancecriteria>>(){});
Comments
Post a Comment