Dealing with Querybuilder results | Community
Skip to main content
Dterner
Level 2
October 16, 2015
Solved

Dealing with Querybuilder results

  • October 16, 2015
  • 2 replies
  • 780 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by JustinEd3

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

2 replies

Dterner
DternerAuthor
Level 2
October 16, 2015

Got it! Thanks Justin.

JustinEd3Adobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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