Using JSONObject in Sightly ? | Community
Skip to main content
paulj62699559
Level 2
October 16, 2015
Solved

Using JSONObject in Sightly ?

Is there a way to use org.apache.sling.commons.json.JSONObject on Sightly ?

For example, if my Java class exposes a JSONObject :

public class Profile extends WCMUse { public JSONObject profileJson; public Profile() { super(); } @Override public void activate() throws Exception { /*  { "user": { "surname": "Paul", "job": "developer" }, "age": 28 } */ String profileJsonString = "{\"user\":{\"surname\":\"Paul\",\"job\":\"developer\"},\"age\":28}"; profileJson = new JSONObject(profileJsonString); } public JSONObject getProfileJson(){ return profileJson; } }

Is there a way to access JSON fields on html files like that :

<div data-sly-unwrap data-sly-use.profile="${'com.test.Profile'}"></div> <h1>Profile</h1> <p>Surname : ${profile.profileJson.user.surname}</p> <p>Age : ${profile.profileJson.age}</p>
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

Just spoke to Eng about this. This is not possible with Slighty. Why not use JavaScript and AJAX. Perform a GET on a Servlet - that returns JSON. Parse the JSON via JavaScript. See: 

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

1 reply

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015

Just spoke to Eng about this. This is not possible with Slighty. Why not use JavaScript and AJAX. Perform a GET on a Servlet - that returns JSON. Parse the JSON via JavaScript. See: 

https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html

Translate