perl處理json的序列化和反序列化
perl可以使用JSON模塊很方便的處理json的序列化和反序列化。先來(lái)一段簡(jiǎn)單的例子:
#! /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 });腳本執(zhí)行后的輸出結(jié)果:

to_json 就是序列化,把hash對(duì)象序列化為json字符串,如果有需要,可以直接把這個(gè)字符串寫入文件。函數(shù)調(diào)用中有一個(gè) pretty => 1 的使用,默認(rèn)不傳入這個(gè)參數(shù)或者 pretty => 0, 結(jié)果如下:
{"arry":[1,2,3,4,5],"desc":"hello world","id":1024,"obj":{"char":["A","B","C"]}}
json中如果要使用 true、false、null這些特殊值,可以如下使用,在之前的那個(gè)腳本中我們繼續(xù)添加舉例:
#! /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 });輸出結(jié)果如下:
{
"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模塊的序列化,下面來(lái)看反序列化的處理,為了說(shuō)明簡(jiǎn)潔,這里直接把上面腳本的輸出結(jié)果加在腳本中,使用perl的DATA句柄讀取數(shù)據(jù),腳本如下:
#! /usr/bin/perl
use v5.14;
use JSON;
use IO::File;
# 讀取json字符串?dāng)?shù)據(jù)
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字符串?dāng)?shù)據(jù)后使用from_json反序列化得到一個(gè)hash引用,然后打印出部分字段的值,結(jié)果如下:
hello world
------------------------------------------------------------
1
0
---------null
------------------------------------------------------------
1
2
3
4
5
可以看到,輸出值是正確的。其中true輸出的是1,false輸出的是0。
到此這篇關(guān)于perl處理json的序列化和反序列化的文章就介紹到這了,更多相關(guān)perl處理json內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Perl使用Tesseract-OCR實(shí)現(xiàn)驗(yàn)證碼識(shí)別教程
這篇文章主要介紹了Perl使用Tesseract-OCR實(shí)現(xiàn)驗(yàn)證碼識(shí)別教程,Tesseract-OCR是一個(gè)開源圖形識(shí)別引擎,需要的朋友可以參考下2014-06-06
perl實(shí)現(xiàn)檢測(cè)服務(wù)器中的服務(wù)是否正常腳本分享
這篇文章主要介紹了perl實(shí)現(xiàn)檢測(cè)服務(wù)器中的服務(wù)是否正常腳本分享,本文腳本用于檢測(cè)Linux服務(wù)器的服務(wù)是否正常,需要的朋友可以參考下2014-10-10
學(xué)習(xí)perl的unless控制結(jié)構(gòu)
在perl的if控制結(jié)構(gòu)中,只有當(dāng)條件表達(dá)式為真時(shí)才執(zhí)行某塊代碼。如果想讓程序塊在條件為假時(shí)才執(zhí)行,此時(shí)可以把if改成unless2013-02-02
在vim中添加perl注釋時(shí)無(wú)法對(duì)齊問(wèn)題的解決方法
在使用vim編輯perl腳本時(shí),每當(dāng)輸入#號(hào)時(shí),#號(hào)都會(huì)跑到行首問(wèn)題,需要的朋友可以參考下2013-02-02
perl跳過(guò)首行讀取文件的實(shí)現(xiàn)代碼
要求直接跳過(guò)第一行,然后讀取后面的內(nèi)容,以下代碼來(lái)自網(wǎng)絡(luò),感謝原作者的辛苦勞動(dòng),順祝新年快樂(lè)2013-02-02

