go through node and store data in a json | Community
Skip to main content
kevinl17371805
Level 2
Oct 26, 2018 9:39 PM UTC
Solved

go through node and store data in a json

hello friends I am in the process of learning the servlet sling and I am

trying to read a parent node and bring all the information it contains to

store it in a json and send it to the view I would appreciate a way to do

thank you acontinuacion I leave an example of what I want to achieve

         

In summary, I want to obtain the data of the subnodes of the subnosdo in and store them in an array with a servlet sling. I appreciate all the help you can give me.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
arunpatidar
arunpatidarAccepted solution
New Participant
Oct 27, 2018 8:47 AM UTC

Hi,

You can use JCR Node API or Resource API to get child resource and adapt to Node API/Value Map

Node (Content Repository for Java Technology API Version 2.0)

Resource (Apache Sling 8 API)

ValueMap and his friends - Experience Delivers

Resource resource = resolver.getResource("/content/testsling/slingmodel");

ValueMap valueMap=resource.adaptTo(ValueMap.class);

response.getWriter().write("Output from ValueMap is First Name: "+valueMap.get("firstName").toString()+" Last Name: "+valueMap.get("lastName").toString()+" Technology: "+valueMap.get("technology").toString()+"");

  UserInfo userInfo = resource.adaptTo(UserInfo.class);

  response.getWriter().write("Output from Sling Model is First Name: "+userInfo.getFirstName()+" Last Name: "+userInfo.getLastName()+" Technology: "+userInfo.getTechnology());

Arun Patidar

2 replies

smacdonald2008
Level 10
Oct 28, 2018 8:48 PM UTC

Nice Response Arun!

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Oct 27, 2018 8:47 AM UTC

Hi,

You can use JCR Node API or Resource API to get child resource and adapt to Node API/Value Map

Node (Content Repository for Java Technology API Version 2.0)

Resource (Apache Sling 8 API)

ValueMap and his friends - Experience Delivers

Resource resource = resolver.getResource("/content/testsling/slingmodel");

ValueMap valueMap=resource.adaptTo(ValueMap.class);

response.getWriter().write("Output from ValueMap is First Name: "+valueMap.get("firstName").toString()+" Last Name: "+valueMap.get("lastName").toString()+" Technology: "+valueMap.get("technology").toString()+"");

  UserInfo userInfo = resource.adaptTo(UserInfo.class);

  response.getWriter().write("Output from Sling Model is First Name: "+userInfo.getFirstName()+" Last Name: "+userInfo.getLastName()+" Technology: "+userInfo.getTechnology());

Translate
Arun Patidar