乱读天书, 不求甚解
周祎骏的个人云笔记
Toggle navigation
乱读天书, 不求甚解
主页
Linux:系统配置
Linux:用户管理
Linux:优化排错
Linux:进程调度
Linux:文件系统
Linux:网络
Linux:系统服务
Linux:安全
Linux:内核
容器:Docker
容器:containerd
容器编排:Kubernetes
IAC:Terraform
大数据:Hadoop
大数据:Zookeeper
大数据:Hbase
消息队列:rsyslog
消息队列:kafka
数据库:MySQL
数据库:MongoDB
搜索引擎:Elasticsearch
时序数据库:OpenTSDB
网站服务:Nginx
编程:Bash
编程:Perl
编程:Python
编程:C
编程:JAVA
编程:Rust
版本控制:gitlab
知识管理:docusaurus
常用小工具
关于我
标签
perl 3.01 用JSON/XML 格式导入导出数据
2016-03-28 12:07:35
75
0
0
admin
>**JSON.pm 和 XML/Sample.pm是常用的perl解析JSON/XML格式的模块,这篇笔记就是介绍这两种模块的。** # JSON.pm ##官方文档: http://search.cpan.org/~makamaka/JSON-2.90/lib/JSON.pm ##直接上code: ```perl #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use JSON; my $hash = { "aa" => "11","bb" => { "cc" => "22", "dd" => "33" } , "ee" => "44" }; #my $result_json = encode_json $hash; #面向方法的写法 my $result_json = JSON->new->encode($hash); #面向对象的写法 #my $result_json = JSON->new->utf8(1)->encode($hash); #utf8 的格式? #my $result_json = JSON->new->pretty(1)->encode($hash); #输出可读的json 字符串 print Dumper $result_json; #my $result_hash = decode_json $result_json; my $result_hash = JSON->new->decode($result_json); print Dumper $result_hash; ``` 运行结果: ```bash [root@test perl]# ./json.pl $VAR1 = '{"bb":{"cc":"22","dd":"33"},"aa":"11","ee":"44"}'; $VAR1 = { 'bb' => { 'cc' => '22', 'dd' => '33' }, 'aa' => '11', 'ee' => '44' }; ``` ##命令行上解析JSON: ```bash [root@test perl]# echo '{"aa":{"bb":2}}' | perl -MJSON -MData::Dumper -lne 'print Dumper decode_json($_);' $VAR1 = { 'aa' => { 'bb' => 2 } }; ``` ##其他小技巧: 有时候我们想把配置文件写成JSON格式,还需要给配置写一些注释,但是JSON格式里当然不能插入注释,这时候我们可以用JSON模块的relaxed功能,它会忽略‘#’后的内容: 配置文件 ```bash [root@test perl]# cat config.js { "host_config" : { #host config "ip" : "0.0.0.0", #ip "hostname" : "localhost" #hostname }, "user" : "admin", #user "limit" : 2 #other config } ``` code: ```perl #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use JSON; chomp(my $string=`cat $ARGV[0]`); my $result_hash = JSON->new->relaxed(1)->decode($string); print Dumper $result_hash; ``` 运行结果: ```bash [root@test perl]# ./read_js.pl config.js $VAR1 = { 'host_config' => { 'ip' => '0.0.0.0', 'hostname' => 'localhost' }, 'user' => 'admin', 'limit' => 2 }; ``` *** #XML::Simple.pm >**写这篇笔记时发现官网已经建议新code不要用这个模块,原因是这个模块太老太臃肿,很多默认选项不合理。官网建议用XML::LibXML(http://search.cpan.org/~shlomif/XML-LibXML-2.0123/LibXML.pod)代替。所以这里只是简单介绍这个模块,以后再补充XML::LibXML的介绍。(¬_¬)** ##官方文档: http://search.cpan.org/~grantm/XML-Simple-2.22/lib/XML/Simple.pm ##直接上code: ```perl #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use XML::Simple; my $hash = { "aa" => "11","bb" => { "cc" => "22", "dd" => "33" } , "ee" => "rr" }; my $xml = XMLout($hash); #把hash 转成xml print $xml; my $new_hash = XMLin("$ARGV[0]"); #也可以是变量,把变量或文件里的字符串(xml格式)转成hash print Dumper $new_hash; ``` 运行成果 ```bash [root@test perl]# cat ./config.xml <host hostname="localhost" ip="127.0.0.1"> <disk space="90%" inode="1%" /> <memory mem="100" swap="20" /> </host> [root@test perl]# ./xml.pl ./config.xml <opt aa="11" ee="rr"> <bb cc="22" dd="33" /> </opt> $VAR1 = { 'memory' => { 'swap' => '20', 'mem' => '100' }, 'disk' => { 'inode' => '1%', 'space' => '90%' }, 'ip' => '127.0.0.1', 'hostname' => 'localhost' }; ```
上一篇:
perl 2.2 面向对象的写法
下一篇:
perl 3.02 巧用 Data::Dumper 导入导出结构化数据
文档导航