I am having a JSON model locally and I want that JSON data into my sling model and upon request to that sling model it should send the whole JSON data to my sightly. But I am unable to get the data in the sightly.
@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ProductsModel {
static final Logger log = LoggerFactory.getLogger(ProductsModel.class);
public List<Product> getProducts() {
ObjectMapper objectMapper = new ObjectMapper();
File jsonFile = new File("absolute path of the json file");
List<Product> products = null;
try {
products = objectMapper.readerForListOf(Product.class).readValue(jsonFile);
} catch (IOException e) {
log.info(e.getMessage());
}
return products;
}
}
Here is my Product class
class Product {
@JsonProperty("image")
private String image;
@JsonProperty("title")
private String title;
@JsonProperty("description")
private String description;
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
}
And here is my HTL code to get the products
<div data-sly-use.model="com.adobe.aem.core.models.ProductsModel">
<div data-sly-list.products="${model.products}">
${products.title}
</div>
</div>
Solved! Go to Solution.
Topics help categorize Community content and increase your ability to discover relevant content.
Views
Replies
Total Likes
did you get the json into the File object? If the about solution doesn't work you can try the following as well
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.JsonNode;
import org.osgi.framework.FrameworkUtil;
// Assuming you have the ObjectMapper instance ready.
ObjectMapper objectMapper = new ObjectMapper();
// Load the JSON file directly into a JsonNode
JsonNode jsonNode = objectMapper.readTree(FrameworkUtil.getBundle(ProductsModel.class)
.getBundleContext()
.getBundle()
.getResource("somedata.json"));
// Use Jackson's TypeFactory to create a collection type
// org.osgi.framework.FrameworkUtil;
TypeFactory typeFactory = objectMapper.getTypeFactory();
CollectionType productListType = typeFactory.constructCollectionType(List.class, Product.class);
// Deserialize the JsonNode into a list of Product objects
List<Product> productObjectList = objectMapper.convertValue(jsonNode, productListType);
See if this one works..
You can try this: putting the file in the resources folder
File file = new File(
this.getClass().getClassLoader().getResource("someName.json").getFile()
);
then you can map the json file into the Object:
ObjectMapper mapper = new ObjectMapper();
SomeClass someClassObj = mapper.readValue(file, SomeClass.class);
Views
Replies
Total Likes
Views
Replies
Total Likes
did you get the json into the File object? If the about solution doesn't work you can try the following as well
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.JsonNode;
import org.osgi.framework.FrameworkUtil;
// Assuming you have the ObjectMapper instance ready.
ObjectMapper objectMapper = new ObjectMapper();
// Load the JSON file directly into a JsonNode
JsonNode jsonNode = objectMapper.readTree(FrameworkUtil.getBundle(ProductsModel.class)
.getBundleContext()
.getBundle()
.getResource("somedata.json"));
// Use Jackson's TypeFactory to create a collection type
// org.osgi.framework.FrameworkUtil;
TypeFactory typeFactory = objectMapper.getTypeFactory();
CollectionType productListType = typeFactory.constructCollectionType(List.class, Product.class);
// Deserialize the JsonNode into a list of Product objects
List<Product> productObjectList = objectMapper.convertValue(jsonNode, productListType);
See if this one works..
Views
Replies
Total Likes
- ui.core
- src
- main
- java
- resources <- here is the folder
- test
- pom.xml
- ui.apps
this is working fine and even while returning i am able to see the log printing the values, but in the HTL while rendering it I am not able to see the value.
<div data-sly-use.model="com.adobe.aem.core.models.ProductsModel">
<div data-sly-list.products="${model.products}">
${products.title}
</div>
</div>
Views
Replies
Total Likes
What are you getting in the "model" in the sightly above? is that a list of Product objects?
Yes, a list of Product objects
Views
Replies
Total Likes
You can refer to this existing thread: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/data-sly-list-iteration-fo...
Heyy @A_H_M_Imrul it is working as expected. Thank you very much.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies