Hi, Newbie question here. When using the query builder interface and get a set of results. I use something like this:
for (Hit hit : result.getHits()) { ValueMap props = hit.getProperties(); String title = props.get("jcr:title").toString(); }
However if the property is an array (such as props.get("cq:tags") ), it returns what appears to be a generic object. How do I extract the string[] value from that object? to get the contents?
Thanks,
- Dmitry
Solved! Go to Solution.
Views
Replies
Total Likes
Hi Dmitry,
The ValueMap interface handles type coercion automatically, you just need to tell it what to do:
String[] values = props.get("jcr:title", String[].class); // will return null if there's no property named "jcr:title"
OR
String[] values = props.get("jcr:title", new String[0]); // will return an empty array if jcr:title isn't a property name
In your example, you should be providing a default value. Otherwise, you could get a null pointer exception if the property doesn't exist.
Regards,
Justin
Views
Replies
Total Likes
Hi Dmitry,
The ValueMap interface handles type coercion automatically, you just need to tell it what to do:
String[] values = props.get("jcr:title", String[].class); // will return null if there's no property named "jcr:title"
OR
String[] values = props.get("jcr:title", new String[0]); // will return an empty array if jcr:title isn't a property name
In your example, you should be providing a default value. Otherwise, you could get a null pointer exception if the property doesn't exist.
Regards,
Justin
Views
Replies
Total Likes
Got it! Thanks Justin.
Views
Replies
Total Likes
Views
Like
Replies
Views
Likes
Replies