Expand my Community achievements bar.

SOLVED

Sightly - Accessing values in String[]

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Level 8

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>

View solution in original post

5 Replies

Avatar

Correct answer by
Level 8

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>

Avatar

Level 5

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

Avatar

Level 10

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. 

Avatar

Level 10
<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>

Avatar

Level 10

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.