0. Installation. 安裝及引入 json
gem install json # rails 中不需要單獨(dú)裝了
require 'json'
1. Reading the file. 讀取文件
file = File.read('./file-name-to-be-read.json')
2. Parsing the file into hash. 解析文件
data_hash = JSON.parse(file)
3. 修改或重新組裝數(shù)據(jù)
data = {}
data["books"] = ...
4. 寫入新文件
File.write('./sample-data.json', JSON.dump(data))