java - Jackson Json: how to convert array to JsonNode and ObjectNode? -


given employee , company class

company {     string companyname; }  employee {     string employeename; } 

and code following

list<employee> e = new arraylist<employee>(); ..... ..... 

i wish can result this

{     "company":{                  "companyname":"cname",                 "employee":[                     {"employeename":"myname1"},                     {"employeename":"myname2"},                     {"employeename":"myname3"}                 ]               } } 

it simple question, quite confusing somethings.... gson , json....

please not propose other library, compulsory need library work. , due circumstance,this class is not wanted.

company {     string companyname;     list<employee> employees; } 

therefore need manually put it,and serialize json string.

edited: @sotirios delimanolis classes declared right way design relationship between classes. however, that's not wanted.

the answer @hsluo correct! , same @sotirios delimanolis mention. totally fulfill question.

and did found way using hashmap

hashmap k = new hashmap(); list<employee> y = new arraylist<employee>(); y...... k.put("records", y); k.put("total", total); 

and return @responbody, result totally same @hsluo answered.

and @sotirios delimanolis, @hsluo me.

objectmapper mapper = new objectmapper(); list<employee> e = new arraylist<employee>(); arraynode array = mapper.valuetotree(e); objectnode companynode = mapper.valuetotree(company); companynode.putarray("employee").addall(array); jsonnode result = mapper.createobjectnode().set("company", companynode); 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -