2

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.

Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
  • I've figured this out already. Will post the answer later since the rules won't allow me to answer within 8 hours of posting the question. – Psycho Punch May 24 '12 at 14:42

1 Answers1

6

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.

Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
  • 1
    For anyone who came across this looking for how to get a value from Map of Maps of Maps of Maps etc (e.g. objectified JSON): PropertyUtils.getProperty(map, "(keyInFirstMap).(keyInSecondMap).(keyInThirdMap)"); A possible warning though: Apache's BeanUtils performance can be bad (see http://rodrigojramirez.com/2013/10/21/beware-of-copyproperties-or-the-30-milliseconds-tale/) - after experiencing it first hand, I'd run a test before using PropertyUtils like above on a heavily exercised code path. – Jan Żankowski Mar 18 '16 at 23:58