Files
ruby-examples/src/37_json.rb
2024-04-04 13:44:51 +09:00

30 lines
507 B
Ruby
Executable File

#!/usr/bin/ruby
# JSON
require 'json'
## 읽기
json = '{"name":"Charlie", "age":14, "human":true}'
data = JSON.parse(json);
p data # {"name"=>"Charlie", "age"=>14, "human"=>true}
data = JSON.parse(File.read('sample.json')) # 파일로부터 읽어 오기
p data
## 내보내기
hash = {name:"Charlie", age: 13, alien:false}
json_str = JSON.generate(hash)
p json_str
array = [0, 's', true, hash]
json_str = JSON.generate(array)
p json_str
END{
# https://ruby-doc.org/3.3.0/exts/json/JSON.html
}