Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

436
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda