我有一个看起来像这样的文本文件
{
"TYPE": "EMAIL",
"ITEMS": [
{
"SENT": "2016-02-01T19:03:02.00Z",
"SUBJECT": "UPCOMING EVENTS: ORIENTATION 2016",
"TIMEZONE": "AUSTRALIA/MELBOURNE",
"CONTENT": "WE'RE PLEASED TO BE WORKING WITH RMIT LINK'S ORIENTATION TEAM AND RUSU TO WELCOME ALL NEW STUDENTS TO CAMPUS THROUGH A SERIES OF EXCITING ORIENTATION EVENTS. THIS EMAIL SERVES AS A NOTIFICATION TO MAKE SURE YOU KNOW WHEN THE MAJOR EVENTS ARE OCCURRING, TO ENSURE THEY DON'T INTERRUPT YOUR WORK AND SO THAT YOU ARE ABLE TO ENCOURAGE ALL NEW STUDENTS TO ATTEND. BRUNSWICK ALL STUDENTS WELCOME, 23 FEBRUARY 12 - 1:30PM BRUNSWICK COURTYARD. BUNDOORA ALL STUDENTS WELCOME, 24 FEBRUARY 12 - 2PM BUNDOORA WEST CONCOURSE. CITY ALL STUDENTS WELCOME, 25 FEBRUARY 11AM - 2:30PM ALUMNI COURTYARD, UNIVERSITY WAY. RUSU WELCOME BASH, 25 FEBRUARY 4PM - 9PM ALUMNI COURTYARD. CITY CLUBS DAY, 3 MARCH 11AM - 2PM ALUMNI COURTYARD, UNIVERSITY WAY."
},
{
"SENT": "2016-03-03T19:03:02.00Z",
"SUBJECT": "PROJECT 1 FIRST TIME MEETING",
"TIMEZONE": "AUSTRALIA/MELBOURNE",
"CONTENT": "EARLY NEXT WEEK IS GOOD FOR US. HOW ABOUT MONDAY 11AM?"
},
{
"SENT": "2016-03-03T19:03:02.00Z",
"SUBJECT": "PROJECT 1 FIRST TIME MEETING",
"TIMEZONE": "AUSTRALIA/MELBOURNE",
"CONTENT": "EARLY NEXT WEEK IS GOOD FOR US. HOW ABOUT TUESDAY 11:30 AM?"
},
}
我试图提取信息,使ITEMS成为哈希数组。这样我就可以访问发送的主题时区等的值。
我试过这个它不起作用。有什么帮助吗?
my @AoH ;
while ( <> ) {
my $rec = {};
for my $field ( split ) {
(my $key, my $value) = split /:/, $field;
$rec->{$key} = $value;
}
push @AoH, $rec;
}
答案 0 :(得分:5)
该JSON数据( JavaScript Object Notation ),但最后一个逗号,
应为结束方括号]
。使用JSON
模块将其解码为Perl数据结构
该程序显示了原理。它只打印每个项目的主题行,但我认为你明白了这个想法
use strict;
use warnings 'all';
use JSON qw/ decode_json /;
my $json = do {
open my $fh, '<:raw', 'text_file.txt' or die $!;
local $/;
<$fh>;
};
my $data = decode_json($json);
my $items = $data->{ITEMS};
for my $item ( @$items ) {
print $item->{SUBJECT}, "\n";
}
UPCOMING EVENTS: ORIENTATION 2016
PROJECT 1 FIRST TIME MEETING
PROJECT 1 FIRST TIME MEETING