java - Trying to map to domain model from json with an object inside -
i have json:
"{\"userid\":2,\"title\":\"jfsdk\",\"content\":\"dskljf\",\"taglist\":{\"name\":\"fysikk\",\"name\":\"matte\",\"name\":\"kjemi\"}}"
that want parse domain model class
public class threadcreatingmodel { int userid; string title; string content; arraylist<tag> taglist; public threadcreatingmodel(){} public threadcreatingmodel(int userid, string title, string content, arraylist<tag> taglist) { this.userid = userid; this.title = title; this.content = content; this.taglist = taglist; } @jsonproperty("userid") public int getuserid() { return this.userid; } public void setuserid(int userid) { this.userid = userid; } @jsonproperty("title") public string gettitle() { return this.title; } public void settitle(string title) { this.title = title; } @jsonproperty("content") public string getcontent() { return this.content; } public void setcontent(string content) { this.content = content; } public arraylist<tag> gettaglist() { return this.taglist; } public void settaglist(arraylist<tag> taglist) { this.taglist = taglist;}
that has tag class inside:
public class tag { public string name; public tag(){ } public tag(string name) { this.name = name; } public string getname() { return this.name; } public void setname(string name) { this.name = name; } }
in mapper class this:
string incoming = "{\"userid\":2,\"title\":\"jfsdk\",\"content\":\"dskljf\",\"taglist\":{\"name\":\"fysikk\",\"name\":\"matte\",\"name\":\"kjemi\"}}"; objectmapper mapper = new objectmapper().configure(deserializationfeature.accept_single_value_as_array,true); threadcreatingmodel tcm = null; try { tcm = mapper.readvalue(incoming, threadcreatingmodel.class); system.out.println(tcm.gettaglist().get(0).name); } catch (ioexception ioe) { system.out.println(ioe.tostring()); system.out.println("failed mapping"); } }
when have "get(0)" last one. if try put 1 java.lang.indexoutofboundsexception: index: 1, size: 1
if remove .configure(deserializationfeature.accept_single_value_as_array,true)
i error:
com.fasterxml.jackson.databind.jsonmappingexception: can not deserialize instance of java.util.arraylist out of start_object token @ [source: java.io.stringreader@66cc75ad; line: 1, column: 47] (through reference chain: com.accenture.studass.domain.threadcreatingmodel["taglist"])
any ? been googeling ages now..
i redesigned frontend-code json have looks instead michaĆ ziober mentioned :
{"userid":2,"title":"foo","content":"bar","taglist":["fysikk","matte"]}
and works wanted do!
Comments
Post a Comment