2024-04-04

This commit is contained in:
2024-04-04 13:44:51 +09:00
parent d444640770
commit fabe7642fa
40 changed files with 2423 additions and 0 deletions

29
src/37_json.rb Executable file
View File

@@ -0,0 +1,29 @@
#!/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
}