Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 22, 2019 02:54
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/8e12593e206d91e9ea96e1530b074283 to your computer and use it in GitHub Desktop.
Save parzibyte/8e12593e206d91e9ea96e1530b074283 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
entero = 1
flotante = 15.2
cadena = "parzibyte.me"
booleano = true
nulo = nil
arreglo = [1, 2, 3]
hash = {"clave" => "valor"}
puts entero.to_json
# 1
puts flotante.to_json
# 15.2
puts cadena.to_json
# "parzibyte.me"
puts booleano.to_json
# true
puts nulo.to_json
# null
puts arreglo.to_json
# [1,2,3]
puts hash.to_json
# {"clave":"valor"}
# Ahora un objeto más grande y complejo
mascota = {
"nombre" => "Maggie",
"edad" => 3,
"peso" => 5.2,
"amigos" => [
{
"nombre" => "Capuchina",
"edad" => 2
},
{
"nombre" => "Guayaba",
"edad" => 1
}
]
}
codificada = mascota.to_json
puts codificada
# {"nombre":"Maggie","edad":3,"peso":5.2,"amigos":[{"nombre":"Capuchina","edad":2},{"nombre":"Guayaba","edad":1}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment