如何序列化Map< String,List< Object>>使用FlexJSON [英] How to serialize Map<String, List<Object>> with FlexJSON

查看:173
本文介绍了如何序列化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.

这篇关于如何序列化Map&lt; String,List&lt; Object&gt;&gt;使用FlexJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆