elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: ¿Eres nuevo? ¿Tienes dudas acerca del funcionamiento de la comunidad? Lee las Reglas Generales


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  [AUXILIO] Insertar string JSON
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [AUXILIO] Insertar string JSON  (Leído 1,454 veces)
Xargam

Desconectado Desconectado

Mensajes: 27


Ver Perfil
[AUXILIO] Insertar string JSON
« 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.


« Última modificación: 27 Septiembre 2019, 15:49 pm por #!drvy » En línea

#!drvy
Moderador
***
Desconectado Desconectado

Mensajes: 5.850



Ver Perfil WWW
Re: [AUXILIO] Insertar string JSON
« Respuesta #1 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


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines