How to serialize Map<String, List<Object>> with FlexJSON

浪尽此生 提交于 2019-12-21 20:52:56

问题


I have a object which I want to serialize to JSON. This object is a map with list of a specific objects. This looks similar to it:

Map<String, List<Object>> map = new Map<String, List<Object>>();

I am using FlexJSON. I can work only with flexjson.JSONSerializer. There are my tries:

JSONSerializer jsonSerializer = new JSONSerializer();
//        jsonSerializer.include("map");
//        jsonSerializer.include("map.myList.object");
jsonSerializer.transform(new MapTransformer(), "map");
jsonSerializer.exclude("*.class");

As you can see I am trying now with Transformer classes but I haven't succeeded. The commented lines will work, as I suppose, if my List<Object> has own name for example myList. But it doesn't haven own name, because it is values of my map.

How can I serialize such object to JSON with FlexJSON?


回答1:


Quoi is correct, and most likely the answer you want to use. However, just so you can learn more about how it works you could do this:

new JSONSerializer().include("values.values").serialize( myMap );

Maps and Lists are containers and have special expressions for the elements contained within them. When you want to talk about the path of the values of a container you use "values" expression in the path. Map's two special expressions "keys" and "values". Other Collections have "values".

Another option is to use wildcards like so:

   new JSONSerializer().include("values.*").serialize( myMap );

You can see wildcards used in Quoi's answer as well to exclude the class property.




回答2:


Try

String jsonString = new JSONSerializer().exclude("*.class").deepSerialize(map);

deepSerialize method performs a deep serialization of the target instance. It will include all collections, maps, and arrays by default so includes are ignored except if you want to include something being excluded by an annotation.



来源:https://stackoverflow.com/questions/12582752/how-to-serialize-mapstring-listobject-with-flexjson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!