Ruby & Rails云莉的技术专题

Ruby: How to read/write JSON Fil

2020-09-22  本文已影响0人  云莉6

0. Installation. 安装及引入 json

gem install json # rails 中不需要单独装了
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. 修改或重新组装数据

data = {}
data["books"] = ...

4. 写入新文件

File.write('./sample-data.json', JSON.dump(data))
上一篇下一篇

猜你喜欢

热点阅读