Convert complex Java map to Json using Gson -


i'm trying convert java map (with list of myclass values) json using gson.

map<integer, list<myclass>> mymap 

here how doing it...

gson gson = new gsonbuilder().enablecomplexmapkeyserialization().create(); string json = gson.tojson(mymap); system.out.println(json); 

it produces json close to, not in format expect/want. here produced:

{"0":[{myclassitem1}, {myclassitem2}, etc], "1":[{myclassitem3}, {myclassitem4}, etc], etc... }

i expect/prefer produces:

[{"0":[{myclassitem1}, {myclassitem2}, etc]}, {"1":[{myclassitem3}, {myclassitem4}, etc]}, etc...]

is possible? if so, how?

well... gson serializes data appropriate form. seems want array of map.entry. try like:

map<integer, list<myclass>> mymap = ...; set<map.entry<integer, list<myclass>>> entryset = mymap.entryset(); object[] data = entryset.toarray(new object[entryset.size()]);  // serialize data variable 

however aware gson might not able serialize map.entry out-of-the-box.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -