解析来我们就可以使用以下命令来安装Ruby JSON 模块:

1
$gem install json

使用 Ruby 解析 JSON

以下为JSON数据,将该数据存储在 input.json 文件中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
 "President""Alan Isaac",
 "CEO""David Richardson",
   
 "India": [
  "Sachin Tendulkar",
  "Virender Sehwag",
  "Gautam Gambhir",
 ],
  
 "Srilanka": [
  "Lasith Malinga",
  "Angelo Mathews",
  "Kumar Sangakkara"
 ],
  
 "England": [
  "Alastair Cook",
  "Jonathan Trott",
  "Kevin Pietersen"
 ]
}

以下的 Ruby 程序用于解析以上 JSON 文件;

1
2
3
4
5
6
7
8
9
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'pp'
  
json = File.read('input.json')
obj = JSON.parse(json)  转为hash  这里的json是字符串不是json对象
  
pp obj

以上实例执行结果为:

1
2
3
4
5
6
7
8
9
10
11
12
{"President"=>"Alan Isaac",
 "CEO"=>"David Richardson",
  
 "India"=>
 ["Sachin Tendulkar""Virender Sehwag""Gautam Gambhir"],
  
"Srilanka"=>
 ["Lasith Malinga ""Angelo Mathews""Kumar Sangakkara"],
  
 "England"=>
 ["Alastair Cook""Jonathan Trott""Kevin Pietersen"]
}
 

最新文章

  1. IT雇员及外包商选择:人品第一
  2. 使用mutt+msmtp在Linux命令行界面下发邮件(续)
  3. jQuery对 动态添加 的元素 绑定事件(on()的用法)
  4. kali2 ssh
  5. Jboss中不能正常的解析EL表达式
  6. Linux终端快捷操作汇总
  7. JS基础与循环
  8. 《Clean Code》 代码简洁之道
  9. 用户注册登录系统 V2.0
  10. 为什么会有这么多python?其实python并不是编程语言!
  11. Solution about MB STAR C4, MB STAR C5 Update and can not test vehicles problems
  12. Linux - 针对用户账号的常用操作
  13. MySQL——索引实现原理
  14. SQL 1
  15. cbv
  16. Struts2 (下)
  17. 编程开发之--java多线程学习总结(6)
  18. LR--用栈实现移进--归约分析(demo)
  19. Windows相关
  20. 【BZOJ2466】[中山市选2009]树 树形DP

热门文章

  1. HDU 1041 Computer Transformation 数学DP题解
  2. CentOS7.0修改主机名(hostname)
  3. 深入Java----集合----BitSet
  4. 【共享单车】—— React后台管理系统开发手记:Router 4.0路由实战演练
  5. shell脚本中数组array常用技巧学习实践
  6. PGM图片格式与代码
  7. Php网站如何优化才好?
  8. iOS的URLScheme
  9. Docker 三大核心工具
  10. python3 configparser对配置文件读写