How to use PropertyUtils to get an element from a list inside a map?

為{幸葍}努か 提交于 2019-12-11 04:07:51

问题


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"]}

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

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

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.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/10738019/how-to-use-propertyutils-to-get-an-element-from-a-list-inside-a-map

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