Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active December 24, 2018 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/5b97fb2c99d2291d975cc5ecbc60324e to your computer and use it in GitHub Desktop.
Save parzibyte/5b97fb2c99d2291d975cc5ecbc60324e to your computer and use it in GitHub Desktop.
Ejemplos de JSON.parse created by parzibyte - https://repl.it/@parzibyte/Ejemplos-de-JSONparse
/*
Decodificar JSON en JavaScript
@author parzibyte
*/
let mascotaComoJSON = '{"nombre":"Guayaba","edad":1,"amigos":[{"nombre":"Maggie","edad":2},{"nombre":"Bichi","edad":1}]}';
let mascota = JSON.parse(mascotaComoJSON);
// Ahora ya es un objeto, podemos acceder a él
console.log("Los amigos de mascota son: ", mascota.amigos);
//Los amigos de mascota son: [ { nombre: 'Maggie', edad: 2 }, { nombre: 'Bichi', edad: 1 } ]
// Lo mismo pasa con los arreglos
let librosComoJSON = '["¿Sueñan los androides con ovejas eléctricas?","El Jilguero","El resplandor","La rebelión de Atlas"]';
let libros = JSON.parse(librosComoJSON);
// Y ya podemos acceder al arreglo
console.log("Libro en la posición 1: ", libros[1]);
//Libro en la posición 1: El Jilguero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment