如何使用PropertyUtils从地图内的列表中获取元素? [英] How to use PropertyUtils to get an element from a list inside a map?

查看:306
本文介绍了如何使用PropertyUtils从地图内的列表中获取元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用PropertyUtils的getProperty的索引符号来检索列表中包含为地图值的元素.这是一个示例(我在这里使用通用语法):

I've been trying to use the indexed notation used for getProperty of PropertyUtils to retrieve an element in a list contained as a map value. Here's an example (I'm using a general syntax here):

map = {"aList": ["elem1", "elem2", "elem3"]}

让我说,我想获取值"elem2",我正在尝试使用:

Let say, I want to get the value "elem2", I'm trying to do it using:

PropertyUtils.getProperty(map, "aList[1]");

但是它似乎不起作用.我总是得到一个空值.还有另一种方法可以做到这一点.明确地说,我知道我可以做一个getProperty("aList").get(0)(当然,在显式转换之后),但是我正在开发一个需要上述代码才能工作的解决方案.

but it doesn't seem to work. I always get a null value. Is there another way to do this. To be clear, I know I can do a getProperty("aList").get(0) (after explicitly casting, of course) but I'm working on a solution that needs the code stated above to work.

推荐答案

好,所以我知道了.下面的代码按我想要的方式工作:

Ok, so I figured it out. The code below works like how I wanted:

PropertyUtils.getProperty(map, "(aList)[1]");

我认为是基于这文档,我在这里要做的是指定aList是键,而不是bean的索引属性.这样的事情.

I think, based on this documentation, what I'm doing here is that I'm specifying that aList is a key and not an indexed property of the bean. Something like that.

这篇关于如何使用PropertyUtils从地图内的列表中获取元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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