Currently, I am developing an application where I need to pass data from a Spring Boot controller to a template using Thymeleaf.
Everything was going smoothly, until I encountered an issue when trying to send JSON data.
I noticed that the double quote (""
) gets changed to "
, which is causing an error in my application.
This is my controller code:
@GetMapping("/statistics")
public String viewStatistics(Model model) {
JSONArray jsonArray = statisticsService.getTaskNamePercentageMap();
System.out.println(jsonArray);
model.addAttribute("taskNamePercentageMap", jsonArray);
return "statistics/main";
}
The output of System.out.println(jsonArray):
[{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]
Here's the JavaScript code in the statistics/main template:
$(document).ready(function () {
var json =[[${taskNamePercentageMap}]];
/*... TO BE CONTINUED ...*/
});
The variable "a" in Chrome developer tab Sources:
var b = JSON.stringify([{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]);
Could anyone provide insights on what may be causing this issue and suggest potential solutions?