Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

442
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda