I'm trying to send a JSON object to a property in javascript using thymeleaf, but when the value is received, all the quotes are converted to ".
In my POJO, I have the following method:
public class MyObject {
public String toJSON() throws IOException {
StringWriter result = new StringWriter();
if (this.getCaseRegistrations().size() > 0) {
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(result, this.caseRegistrations);
}
return result.toString();
}
And elsewhere, that class is created and ready to use and sent to the ModelAndView in the Spring controller:
mav.addObject("myObject", myObject);
In my html page, I have the following:
<script type="text/javascript">
$(document).ready(function() {
let registrations = [[ ${myObject.toJSON()} ]]
console.log(registrations);
});
Unfortunately, when this runs, javascript fails on the assignment with a complaint about &. Looking at the Source, I see the following:
let registrations = [{"prop1":"val1", ...}];
When I look at the string on the java side (in debug mode), the value looks correct
[{ "prop1": "val1", ...}]
I've tried various things, such as let registrations = '[[ ${myObject.toJSON()} ]]' (using the backquote here) and let registrations = decudeURI[[ ${myObject.toJSON()} ]]) and [# th:utext="${myObject.toJSON()}"/];. That last attempt (with th:utext), failed completely.
What is the proper way to convert a POJO to a JSON and the assign it to a variable in javascript on an html page? (Java-Spring-Thymeleaf)
i dont have much experience in this tech but i can help you to give scenario is that
strrplace() i dont if you know some another function [http://w3schools.com/php/func_string_str_replace.asp] look ta here it will replace all the brackets. see first argument will be what you want to replace and second will be with what you want replace .in my case it was like,
$user=str_replace("'[',']','{','}'",(my json variable data));