In controller I put simple java object, mapped to JSON using jackson.
Station station = stationRepo.findFirstByCodeEquals(320007);
ObjectMapper objectMapper = new ObjectMapper();
String JSONstation = objectMapper.writeValueAsString(station);
model.addAttribute("station",JSONstation);
In front end I use Thymeleaf to get this object in tag:
<p id="test" th:text="${station}">Test 1</p>
<p id="test2">Test 2</p>
<p id="test3">Test 3</p>
And I simply get this JSON object in javascript using document.getElementById("test").innerText, and parse it into js object.
var JSONtest = "[[${station}]]";
var JSONstation = document.getElementById("test").innerText;
document.getElementById("test2").innerHTML = typeof JSONtest;
var jsStation = JSON.parse(JSONstation);
document.getElementById("test3").innerHTML = JSONtest.rusName;
But when I thy to get JSON object from thymeleaf using var JSONtest = "[[${station}]]" I can`t parse it into js object, but they are the same. What am I do wrong in this code?
var JSONtest = `[[${station}]]`;- you need to enclose the string in backticks to use the${..}interpolation syntax