Solved
How to read a JSON file and map it to the sling model
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>
So my output is just blank, not showing anything...please help...