美文网首页Ruby & Rails云莉的技术专题
Ruby: How to read/write JSON Fil

Ruby: How to read/write JSON Fil

作者: 云莉6 | 来源:发表于2020-09-22 18:21 被阅读0次

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))

相关文章

网友评论

    本文标题:Ruby: How to read/write JSON Fil

    本文链接:https://www.haomeiwen.com/subject/tqxgyktx.html