Ruby JSON操作
2024-08-29 02:39:01
解析来我们就可以使用以下命令来安装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" ] } |
最新文章
- IT雇员及外包商选择:人品第一
- 使用mutt+msmtp在Linux命令行界面下发邮件(续)
- jQuery对 动态添加 的元素 绑定事件(on()的用法)
- kali2 ssh
- Jboss中不能正常的解析EL表达式
- Linux终端快捷操作汇总
- JS基础与循环
- 《Clean Code》 代码简洁之道
- 用户注册登录系统 V2.0
- 为什么会有这么多python?其实python并不是编程语言!
- Solution about MB STAR C4, MB STAR C5 Update and can not test vehicles problems
- Linux - 针对用户账号的常用操作
- MySQL——索引实现原理
- SQL 1
- cbv
- Struts2 (下)
- 编程开发之--java多线程学习总结(6)
- LR--用栈实现移进--归约分析(demo)
- Windows相关
- 【BZOJ2466】[中山市选2009]树 树形DP