perl處理json的序列化和反序列化
perl可以使用JSON模塊很方便的處理json的序列化和反序列化。先來一段簡單的例子:
#! /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模塊的序列化,下面來看反序列化的處理,為了說明簡潔,這里直接把上面腳本的輸出結(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常量、多維數(shù)組及變量的初始化的實(shí)例代碼
perl常量、多維數(shù)組及變量的初始化的例子,供大家學(xué)習(xí)參考2013-02-02perl 調(diào)試命令的相關(guān)知識(shí)小結(jié)
有關(guān) perl 調(diào)試命令的相關(guān)知識(shí),有需要的朋友可以參考下2013-02-02Perl語言入門學(xué)習(xí)指南及實(shí)用示例
Perl廣泛應(yīng)用于系統(tǒng)管理、Web開發(fā)、網(wǎng)絡(luò)編程和數(shù)據(jù)處理等領(lǐng)域,本文將帶您入門Perl語言,介紹其基本語法、常用功能及實(shí)用示例,感興趣的朋友跟隨小編一起看看吧2024-07-07Perl實(shí)現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db
這篇文章主要介紹了Perl實(shí)現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db,本文實(shí)現(xiàn)了批量刪除Thumbs.db文件,需要的朋友可以參考下2014-12-12使用perl實(shí)現(xiàn)拆分?jǐn)?shù)據(jù)表(mysql)并遷移數(shù)據(jù)實(shí)例
這篇文章主要介紹了使用perl實(shí)現(xiàn)拆分?jǐn)?shù)據(jù)表(mysql)并遷移數(shù)據(jù)實(shí)例,本文提供了3個(gè)腳本,分別用于拆分?jǐn)?shù)據(jù)表、遷移數(shù)據(jù)、插入測(cè)試數(shù)據(jù),需要的朋友可以參考下2014-10-10