Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

434
Views
Sending a Jackson converted string or JSON object to HTML page using thyemeleaf

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 = [{&quot;prop1&quot;:&quot;val1&quot;, ...}];

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)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

i dont have much experience in this tech but i can help you to give scenario is that

  1. Capture the json data before sending to js.
  2. then use something like 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));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!