Created
February 7, 2016 20:02
-
-
Save yupadhyay/aa9739d4235c66aa9b18 to your computer and use it in GitHub Desktop.
Access String Array in Sightly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ackage 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment