issues about json format returned by spring restful? -
class entitydataset { public list<entityrow> getdata() { return rowlist; } class entityrow { private map<string, object> rowdata; public map<string, object> getrowdata() { return rowdata; } }
i wrote method in restful controller below.
@requestmapping(value = "/entitydata", method = requestmethod.post, headers = "accept=application/json") public entitydataset getentitydata(@requestbody queryobject query) throws exception{ return entitymanager.generateentitydatabyname(query); }
the returned json looked below.
{ "data": [ { "rowdata": { "fiscal_year_end": null, "home_country": null } }, { "rowdata": { "fiscal_year_end": null, "home_country": "10" } }, ..................
but want below.
{ "data": [ { "fiscal_year_end": null, "home_country": null }, { "fiscal_year_end": null, "home_country": "10" },
how can implement it? thanks.
it can done (if using jackson) using @jsonanysetter
, @jsonanygetter
annotations in entityrow class:
import java.util.arraylist; import java.util.list; import com.fasterxml.jackson.annotation.jsonproperty; public class entitydataset { @jsonproperty("data") private list<entityrow> rowlist; /** * @return rowlist */ public list<entityrow> getrowlist() { return rowlist; } /** * @param rowlist rowlist set */ public void setrowlist(list<entityrow> rowlist) { this.rowlist = rowlist; } /** * * @param row */ public void add(entityrow row){ if(this.rowlist == null){ this.rowlist = new arraylist<entityrow>(); } this.rowlist.add(row); } }
this key class, entityrow @jsonanysetter , @jsonanygetter annotations
import java.util.map; import com.fasterxml.jackson.annotation.jsonanygetter; import com.fasterxml.jackson.annotation.jsonanysetter; public class entityrow { private map<string, object> rowdata; /** * @param rowdata rowdata set */ @jsonanysetter public void setrowdata(map<string, object> rowdata) { this.rowdata = rowdata; } @jsonanygetter public map<string, object> getrowdata() { return rowdata; } }
a main test it:
import java.util.hashmap; import java.util.map; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.objectmapper; public class test4 { public static string getjsonstring(object o){ objectmapper mapper = new objectmapper(); //for testing try { //convert object json string string jsoninstring = mapper.writevalueasstring(o); //system.out.println(jsoninstring); return jsoninstring; } catch (jsonprocessingexception e){ e.printstacktrace(); } return null; } public static void main(string args[]){ map<string, object> map = new hashmap<string, object>(); map.put("fiscal_year_end", null); map.put("home_country", null); map<string, object> map2 = new hashmap<string, object>(); map2.put("fiscal_year_end", null); map2.put("home_country", "10"); entityrow row1 = new entityrow(); row1.setrowdata(map); entityrow row2 = new entityrow(); row2.setrowdata(map2); entitydataset entitydataset = new entitydataset(); entitydataset.add(row1); entitydataset.add(row2); system.out.println(test4.getjsonstring(entitydataset)); } }
finally, output:
{"data": [ { "fiscal_year_end":null, "home_country":null }, { "fiscal_year_end":null, "home_country":"10" } ] }
Comments
Post a Comment