Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: Xargam en 26 Septiembre 2019, 04:06 am



Título: [AUXILIO] Insertar string JSON
Publicado por: Xargam en 26 Septiembre 2019, 04:06 am
Este código me está enloqueciendo:

Código
  1. function mostrar(jsonStr : string) {
  2.    console.log(typeof jsonStr );
  3.    console.log(jsonStr);
  4.    let jsonObj = JSON.parse(jsonStr );
  5.    alert(`Id: ${jsonObj.Id} - Marca: ${jsonObj.Marca} -  Precio: ${jsonObj.Precio} -  Color: ${jsonObj.Color} - \
  6.    Modelo: ${jsonObj.Modelo}.`);
  7. }
  8. function leer() {
  9.    let tabla = "<table border='1'><thead><th>Id</th><th>Marca</th><th>Precio</th><th>Color</th><th>Modelo</th>\
  10.    <th>Action</th></thead><tbody>";
  11.    let xhttp = new XMLHttpRequest();
  12.    xhttp.onreadystatechange = () => {
  13.        if( xhttp.readyState == 4 && xhttp.status == 200) {
  14.            let jsonObj = JSON.parse(xhttp.responseText);
  15.            for(let i = 0 ; i < jsonObj.length ; i++) {
  16.                let g  = JSON.stringify(jsonObj[i]);
  17.                console.log(typeof g);
  18.                console.log(g);
  19.                tabla += "<tr>";
  20.                tabla += `<td>${jsonObj[i].Id}</td>`;
  21.                tabla += `<td>${jsonObj[i].Marca}</td>`;
  22.                tabla += `<td>${jsonObj[i].Precio}</td>`;
  23.                tabla += `<td>${jsonObj[i].Color}</td>`;
  24.                tabla += `<td>${jsonObj[i].Modelo}</td>`;
  25.                console.log(JSON.stringify(jsonObj[i]));
  26.                tabla += "<td><input type='button' value='ver' onclick=\"mostrar(\'"+JSON.stringify +"\')\"</td>"; // AQUI
  27.                tabla += "</tr>";
  28.            }
  29.            tabla += "</tbody></table>";
  30.            (<HTMLDivElement>document.getElementById("result")).innerHTML += tabla;
  31.        }
  32.    }
  33.    xhttp.open("GET", "./json_test.php", true);
  34.    xhttp.send();
  35. }
  36.  

Donde esta el aquí no hay manera de que me reconozca las comillas que rodean al parametro de la funcion mostrar ( Que es un objeto json convertido a string). Probé de mil maneras y si consigo que funcione lo hace con comportamientos extraños. En la funcion mostrar en vez de recibir un string recibo un object. Evidentemente por la falta de las comillas que rodean el parametro. Ayuda por fa.


Título: Re: [AUXILIO] Insertar string JSON
Publicado por: #!drvy en 27 Septiembre 2019, 16:04 pm
El problema que tienes seguramente es que stringify también imprime comillas y estas escapan a las comillas dobles de fuera. Una solución sería codificar el stringify y descodificarlo en la funcion mostrar

Código
  1. let attr_mostrar = encodeURIComponent(JSON.stringify(jsonObj[i]));
  2. table += "<td><input type='button' value='ver' onclick=\"mostrar(\'"+attr_mostrar+"\')\"></td>";


Código
  1. function mostrar(jsonStr : string) {
  2.    jsonStr = decodeURIComponent(jsonStr) || '';
  3.    // ....


Saludos