2024-04-04
This commit is contained in:
29
src/37_json.rb
Executable file
29
src/37_json.rb
Executable 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
|
||||
}
|
||||
Reference in New Issue
Block a user