如何序列化Map< String,List< Object>>使用FlexJSON

如何序列化Map< String,List< Object>>使用FlexJSON

问题描述:

我有一个想要序列化为JSON的对象。此对象是包含特定对象列表的地图。这与它类似:

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>>();

我正在使用FlexJSON。我只能使用 flexjson.JSONSerializer 。有我的尝试:

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");

正如您所看到的,我正在尝试使用 Transformer 课程,但我没有成功。正如我想的那样,如果我的 List< Object> 具有自己的名称,例如 myList ,则注释行将起作用。但它没有自己的名字,因为我的地图是

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.

我如何序列化这样的使用FlexJSON反对JSON?

How can I serialize such object to JSON with FlexJSON?

Quoi是正确的,很可能是您想要使用的答案。但是,为了让您可以了解它的工作原理,您可以这样做:

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 );

地图和列表是容器,包含其中包含的元素的特殊表达式。如果要讨论容器值的路径,请在路径中使用值表达式。 Map的两个特殊表达式keys和values。其他收藏集具有值。

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 );

您可以在Quoi的答案中看到用于排除类属性的通配符。

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