perl处理json的序列化和反序列化

perl可以使用JSON模块很方便的处理json的序列化和反序列化。先来一段简单的例子:

perl 复制代码
#! /usr/bin/perl
use v5.14;
use JSON;
use IO::File;

my $info = {
	id => 1024,
	desc => 'hello world',
	arry => [1, 2, 3, 4, 5],
	obj => {
		char => [ 'A', 'B', 'C' ]
	}
};

say to_json($info, { pretty => 1 });

脚本执行后的输出结果:

to_json 就是序列化,把hash对象序列化为json字符串,如果有需要,可以直接把这个字符串写入文件。函数调用中有一个 pretty => 1 的使用,默认不传入这个参数或者 pretty => 0, 结果如下:

javascript 复制代码
{"arry":[1,2,3,4,5],"desc":"hello world","id":1024,"obj":{"char":["A","B","C"]}}

json中如果要使用 true、false、null这些特殊值,可以如下使用,在之前的那个脚本中我们继续添加举例:

perl 复制代码
#! /usr/bin/perl
use v5.14;
use JSON;
use IO::File;

my $info = {
	id => 1024,
	desc => 'hello world',
	arry => [1, 2, 3, 4, 5],
	obj => {
		char => [ 'A', 'B', 'C' ]
	},
	other_1 => {
		test_true => \1,
		test_false => \0,
		test_null => undef
	},
	other_2 => {
		test_true => JSON::true,
		test_false => JSON::false,
		test_null => JSON::null
	}
};

say to_json($info, { pretty => 1 });

输出结果如下:

javascript 复制代码
{
   "other_2" : {
      "test_true" : true,
      "test_null" : null,
      "test_false" : false
   },
   "obj" : {
      "char" : [
         "A",
         "B",
         "C"
      ]
   },
   "arry" : [
      1,
      2,
      3,
      4,
      5
   ],
   "id" : 1024,
   "other_1" : {
      "test_false" : false,
      "test_null" : null,
      "test_true" : true
   },
   "desc" : "hello world"
}

在脚本中可以清楚的看到两种表示方法:

了解了JSON模块的序列化,下面来看反序列化的处理,为了说明简洁,这里直接把上面脚本的输出结果加在脚本中,使用perl的DATA句柄读取数据,脚本如下:

perl 复制代码
#! /usr/bin/perl
use v5.14;
use JSON;
use IO::File;

# 读取json字符串数据
my $json_str = join('', <DATA>);
# 反序列化操作
my $json = from_json($json_str);

say $json->{desc};
say '-' x 60;

say $json->{other_1}{test_true};
say $json->{other_1}{test_false};
unless (defined $json->{other_1}{test_null}) {
    say '---------null';
}
say '-' x 60;
say for @{ $json->{arry} };

__DATA__
{
   "id" : 1024,
   "desc" : "hello world",
   "other_1" : {
      "test_null" : null,
      "test_false" : false,
      "test_true" : true
   },
   "obj" : {
      "char" : [
         "A",
         "B",
         "C"
      ]
   },
   "other_2" : {
      "test_false" : false,
      "test_null" : null,
      "test_true" : true
   },
   "arry" : [
      1,
      2,
      3,
      4,
      5
   ]
}

脚本读取json字符串数据后使用from_json反序列化得到一个hash引用,然后打印出部分字段的值,结果如下:

javascript 复制代码
hello world
------------------------------------------------------------
1
0
---------null
------------------------------------------------------------
1
2
3
4
5

可以看到,输出值是正确的。其中true输出的是1,false输出的是0。

相关推荐
孙克旭_5 小时前
PXE_Kickstart_无人值守自动化安装系统
linux·运维·自动化
皓月盈江6 小时前
Linux电脑本机使用小皮面板集成环境开发调试WEB项目
linux·php·web开发·phpstudy·小皮面板·集成环境·www.xp.cn
深井冰水6 小时前
mac M2能安装的虚拟机和linux系统系统
linux·macos
leoufung7 小时前
内核内存锁定机制与用户空间内存锁定的交互分析
linux·kernel
IT专业服务商8 小时前
联想 SR550 服务器,配置 RAID 5教程!
运维·服务器·windows·microsoft·硬件架构
海尔辛8 小时前
学习黑客5 分钟小白弄懂Windows Desktop GUI
windows·学习
gushansanren8 小时前
基于WSL用MSVC编译ffmpeg7.1
windows·ffmpeg
忧虑的乌龟蛋8 小时前
嵌入式Linux I2C驱动开发详解
linux·驱动开发·嵌入式·iic·i2c·读数据·写数据
I_Scholar9 小时前
OPENSSL-1.1.1的使用及注意事项
linux·ssl
伐尘9 小时前
【Qt】编译 Qt 5.15.x For Windows 基础教程 Visual Studio 2019 MSVC142 x64
windows·qt·visual studio