Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 22, 2019 02:57
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/0c7f457832d4c6873828ca7ced083cb3 to your computer and use it in GitHub Desktop.
Save parzibyte/0c7f457832d4c6873828ca7ced083cb3 to your computer and use it in GitHub Desktop.
=begin
Codificar y decodificar JSON (JavaScript Object Notation)
en el lenguaje de programación Ruby
@author parzibyte
=end
mascota = {
"nombre" => "Maggie",
"edad" => 3,
"peso" => 5.2,
"amigos" => [
{
"nombre" => "Capuchina",
"edad" => 2
},
{
"nombre" => "Guayaba",
"edad" => 1
}
]
}
codificada = mascota.to_json
# Para decodificar, usamos JSON.parse
decodificada = JSON.parse(codificada)
puts decodificada["nombre"]
# Maggie
puts JSON.parse('1.5')
# 1.5
nulo = JSON.parse('null')
if nulo === nil
puts "Es nulo"
end
# Es nulo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment