randomuser1
randomuser1

Reputation: 2803

how can I access the string array stored in jcr node?

In my jcr node I have the key subpage and it holds the value of type String[]:

{"title":"some title1", "url":"some url1"}
{"title":"some title2", "url":"some url2"}
{"title":"some title3", "url":"some url3"}
{"title":"some title4", "url":"some url4"}

how can I access it in java?

I tried:

ValueMap contentValueMap = resource.getValueMap();

String subpages = contentValueMap.get("subpage", String.class);

System.out.println(subpages); 

but it only prints the first string:

{"title":"some title1", "url":"some url1"}

how can I reach the rest of them?

Upvotes: 1

Views: 5806

Answers (2)

Saravana Prakash
Saravana Prakash

Reputation: 1323

As awd mentions

String[] subPages = contentValueMap.get("subpage", String[].class);

works and is the recommended solution. This is accessing data at Sling layer. Just incase you need to dive deeper and access at JCR layer, code will look like

Node node=resource.adaptTo(Node.class);
Value[] subPages = node.getProperty("subpage").getValues();

This will be helpful for Node level operations. But it is advisable to work at higher layers at Sling or AEM.

Upvotes: 4

awd
awd

Reputation: 2322

this should work-

String[] subpages = contentValueMap.get("subpage", String[].class);

Upvotes: 6

Related Questions