java - Google gson.toJson(List) returning response as string instead of array -
i trying utilize jsonobject convert java object string. next code using add together properties :
jsonobject jsonobject = new jsonobject(); jsonobject.addproperty("id", favoritewrapper.getid()); jsonobject.addproperty("menuitemid", favoritewrapper.getmenuitemid()); jsonobject.addproperty("displayname", favoritewrapper.getdisplayname()); jsonobject.addproperty("description", favoritewrapper.getdescription()); jsonobject.addproperty("alias", favoritewrapper.getalias()); gson gson = new gson(); jsonobject.addproperty("condiments", gson.tojson(favoritewrapper.getcondiments()));
here lastly property condiments list of long values , next response retrieved:
[ { "id": 1, "menuitemid": 1, "displayname": "ham", "description": "ham", "alias": "ham", "condiments": "[1,8,34,2,6]" } ]
expected output next different condiments:
[ { "id": 1, "menuitemid": 1, "displayname": "ham", "description": "ham", "alias": "ham", "condiments": [1,8,34,2,6] } ]
what should condiments json array rather string ?
i found reply problem. used jsonarray , jsonprimitive accomplish required response:
jsonobject jsonobject = new jsonobject(); jsonobject.addproperty("id", favoritewrapper.getid()); jsonobject.addproperty("menuitemid", favoritewrapper.getmenuitemid()); jsonobject.addproperty("displayname", favoritewrapper.getdisplayname()); jsonobject.addproperty("description", favoritewrapper.getdescription()); jsonobject.addproperty("alias", favoritewrapper.getalias()); jsonarray condiments = new jsonarray(); (long condimentid : favoritewrapper.getcondiments()) { condiments.add(new jsonprimitive(condimentid)); } jsonobject.add("condiments", condiments); jsonobjects.add(jsonobject);
java json gson arrays jsonobject
No comments:
Post a Comment