unable to access JSONObject in sightly html file | Community
Skip to main content
Level 3
January 20, 2016
Solved

unable to access JSONObject in sightly html file

  • January 20, 2016
  • 17 replies
  • 11707 views

Hi,

I am trying to access JSONObject  in sightly html,but the data is not displayed.

Below is the sightly java file,where "result" variable is holding JSON string.

public class TestSightly extends WCMUse {

   public String result;
   public JSONObject jsonObject;   
    
    @Override
    public void activate() throws Exception {
        TestLocation testLocation = new TestLocation();
        result = testLocation.testMessage();
        jsonObject = new JSONObject(result);
        
    }

     public String getResult(){
        return result;
    }
    
    public JSONObject getJsonObject(){
        return jsonObject
    }
       
}

HTML File:

<div data-sly-include="/libs/wcm/core/components/init/init.jsp"></div>

<div data-sly-use.model="${'com.test.core.models.TestSightly'}">
 <div>${model.result}</div>
 <div>${model.jsonObject}</div>

</div>

 

With the above "model.result" is displaying the string data(json formatted data),but "model.jsonObject" is displaying nothing.

 

Thanks & Regards

swati

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 swathiv54399501

Issue fixed.Found alternate way.

Instead of using org.apache.sling.commons.json.JSONObject API, I changed to GSON api and converted json to javabean object and then accessing in sightly file.

But using JSONObject in sightly is still not possible.

swati

17 replies

GK-007
Level 9
January 20, 2016

Are you seeing any error in error.log when this expression is eveluating??

-Kishore

Jitendra_S_Toma
Level 10
January 20, 2016

Just validate result string whether it is correct json or not. And, if there is any invalid json exception, you will have some error message in error.log

Jitendra

Level 3
January 20, 2016

No errors in the error log and even json is correct as I am getting the json of jcr node with infinity.json,so I dont think there will be errors in the json.

I am able to parse the individual keys of json string in sightly java file and print the values like below,

String posts = json.getJSONObject("testkey1");
 String  test = posts.getString("testkey2");

But unable to access direct JSONObject.

I am trying to avoid parsing keys and values of json in java file (because if I do it ,I have to hardcode keys of json in java file)

Jitendra_S_Toma
Level 10
January 20, 2016

I suspect that result string is not a jsonObject. it is a jsonArray. Would you mind returning jsonArray than JSONObject?.

Jitendra

Level 3
January 20, 2016

result is string and it is in json format.Once I get it the result, I am converting it into JSONObject.

As you suggested, I changed it to JSONArray but no luck.

JSONArray jsonArray = new JSONArray(result);

GK-007
Level 9
January 20, 2016

In which format this "result" string is constructed and passed to JSONObject??

Below is the javadoc details of JSONObject(String).Please refer.

JSONObject

public JSONObject(String string) throws JSONException
Construct a JSONObject from a string. This is the most commonly used JSONObject constructor.

string - A string beginning with { (left brace) and ending with } (right brace).

Throws:

JSONException - If there is a syntax error in the source string.
Level 3
January 20, 2016

I am using JSONObject with string as parameter to construct JSONObject.As I mentioned before I am getting the json string from infinity.json(from jcr nodes). Below is the sample json string which I am converting to JSONObject,

 

{  
   "jcr:primaryType":"nt:unstructured",
   "testkey1":{  
      "jcr:primaryType":"nt:unstructured",
      "test1":"al",
      "test2":"Alabama",
      "test3":"US"
      
      
   },
   "testkey2":{  
      "jcr:primaryType":"nt:unstructured"
   }
}
 

Thanks,

Swati

Jitendra_S_Toma
Level 10
January 20, 2016

Swathi,

I think the issue is not with the Sightly POJO file. Do you mind testing same thing with JSP?.  It is just a guess that something strange happening because of Sightly.

Jitendra

Level 3
January 20, 2016

I already tried with jsp, it is working fine.When I tried to convert that jsp to sightly html file,I faced this issue.

Jitendra_S_Toma
Level 10
January 20, 2016

Yes. I had the same feeling. Try using context="unsafe" while rendering json object. Just try that.

for instance 

${jsonObject @ context='unsafe'}

Jitendra