Sightly - Accessing values in String[] | Community
Skip to main content
New Participant
February 7, 2016
Solved

Sightly - Accessing values in String[]

  • February 7, 2016
  • 5 replies
  • 2856 views

I've got a property as follows

items="[{"linkLabel":"Monkey"\,"anchor":"#monkey"}, "linkLabel":"Bear"\,"anchor":"#bear"},{"linkLabel":"Lion"\,"anchor":"#lion"}]"

 

When I loop through it in Sightly I see the values:

Sightly Code:

<div data-sly-list.itemList="${properties.items}">

    <div>item: ${itemList}</div>
</div>

Output:

item: {"linkLabel":"Monkey","anchor":"#monkey"}
item: {"linkLabel":"Bear","anchor":"#bear"}
item: {"linkLabel":"Lion","anchor":"#lion"}

 

 

How can I get the values of 'linkLabel' and 'anchor' individually?

Thank you in advance for your help.

-Dean

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 leeasling

I wrote a class to handle this.

package com.project.sightly; import com.adobe.cq.sightly.WCMUse; import org.apache.sling.commons.json.JSONObject; import java.util.*; public class JsonHelper extends WCMUse { private String[] json; @Override public void activate() throws Exception { json = this.get("json", String[].class); } public List<Map<String, String>> getValues() { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); if (json != null) { for (String value : json) { Map<String, String> column = parseItem(value); if (column != null) { results.add(column); } } } return results; } private Map<String, String> parseItem(String value) { Map<String, String> columnMap = new HashMap<String, String>(); try { JSONObject parsed = new JSONObject(value); for (Iterator<String> iter = parsed.keys(); iter.hasNext();) { String key = iter.next(); String innerValue = parsed.getString(key); columnMap.put(key, innerValue); } return columnMap; } catch (Exception e) { } return null; } }

You can use it like this:

<div data-sly-use.multi="${'com.project.sightly.MultiField' @ json=properties.items}"> <div data-sly-repeat="${multi.values}"> <h2>${item['linkLabel']} - ${item['anchor']}</h2> </div> </div>

5 replies

Amit_Kumar
New Participant
February 8, 2016

smacdonald2008 wrote...

In my view - (not everyone agrees) - making use of Java on back-end with use of Sightly gives you a lot of power instead of trying to duplicate syntax in Sightly. Let Java handle collections, etc. 

 

deana66659071 I agree with smacdonald2008

though you can use the code provided my me. It will solve your problem.

Amit_Kumar
New Participant
February 8, 2016
<div data-sly-list.outerlistelement="${properties.items}"> <div data-sly-list.innerlistelement="${outerlistelement}" data-sly-unwrap> <div>linkLabel: ${innerlistelement.linkLabel} anchor: ${innerlistelement.anchor}</div> </div> </div>
smacdonald2008
New Participant
February 7, 2016

In my view - (not everyone agrees) - making use of Java on back-end with use of Sightly gives you a lot of power instead of trying to duplicate syntax in Sightly. Let Java handle collections, etc. 

New Participant
February 7, 2016

Thank you for your help.  I've done something similar, just wish there was an easier way to handle this in Sightly.

I like the way you approached this with this "generic" class.

Thanks again.

-Dean

leeaslingAccepted solution
New Participant
February 7, 2016

I wrote a class to handle this.

package com.project.sightly; import com.adobe.cq.sightly.WCMUse; import org.apache.sling.commons.json.JSONObject; import java.util.*; public class JsonHelper extends WCMUse { private String[] json; @Override public void activate() throws Exception { json = this.get("json", String[].class); } public List<Map<String, String>> getValues() { List<Map<String, String>> results = new ArrayList<Map<String, String>>(); if (json != null) { for (String value : json) { Map<String, String> column = parseItem(value); if (column != null) { results.add(column); } } } return results; } private Map<String, String> parseItem(String value) { Map<String, String> columnMap = new HashMap<String, String>(); try { JSONObject parsed = new JSONObject(value); for (Iterator<String> iter = parsed.keys(); iter.hasNext();) { String key = iter.next(); String innerValue = parsed.getString(key); columnMap.put(key, innerValue); } return columnMap; } catch (Exception e) { } return null; } }

You can use it like this:

<div data-sly-use.multi="${'com.project.sightly.MultiField' @ json=properties.items}"> <div data-sly-repeat="${multi.values}"> <h2>${item['linkLabel']} - ${item['anchor']}</h2> </div> </div>