Search code examples
javascriptjavajsonspring-bootthymeleaf

Spring Boot: getting wrong format of double qoutes


I am writing an app in which I need to send some data from Spring Boot controller to template by Thymeleaf.

I did not have problems until I wanted to send some JSON.

Double qoute(") is changed to ".

Because of that I am getting an error.

Controller:

@GetMapping("/statistics")
public String viewStatistics(Model model) {
    JSONArray jsonArray = statisticsService.getTaskNamePercentageMap();
    System.out.println(jsonArray);
    model.addAttribute("taskNamePercentageMap", jsonArray);
    return "statistics/main";
}

System.out.println(jsonArray) output:

[{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]

JavaScript code in statistics/main template:

$(document).ready(function () {
        var json =[[${taskNamePercentageMap}]];
        /*... TO BE CONTINUED ...*/
    });

Variable "a" in Chrome developer tab Sources:

var b = JSON.stringify([{"low":33,"name":"Tenis"},{"low":100,"name":"Rugby"}]);

Can someone tell me where is the problem and how to fix it?


Solution

  • Try this

    var json = [(${taskNamePercentageMap})];
    

    From Thymeleaf 3.0 docs

    Note that, while [[...]] corresponds to th:text (i.e. result will be HTML-escaped), [(...)] corresponds to th:utext and will not perform any HTML-escaping.