Perl External JSON API 海康威视人脸扫描

Perl External JSON API Hikvision Face scan

您好,请帮助我尝试使用 perl 从 Hikvision Face Scan 获取 AcsEvent。 现在数据 return 是。

{
    "AcsEvent": {
        "searchID": "1",
        "responseStatusStrg": "MORE",
        "numOfMatches": 5,
        "totalMatches": 13430,
        "InfoList": [
            {
                "major": 2,
                "minor": 1024,
                "time": "2021-12-02T10:45:33+07:00",
                "netUser": "",
                "remoteHostAddr": "0.0.0.0",
                "cardNo": "",
                "cardType": 0,
                "name": "",
                "reportChannel": 0,
                "cardReaderKind": 0,
                "cardReaderNo": 0,
                "doorNo": 0,
                "verifyNo": 0,
                "alarmInNo": 0,
                "alarmOutNo": 0,
                "caseSensorNo": 0,
                "RS485No": 0,
                "multiCardGroupNo": 0,
                "deviceNo": 0,
                "employeeNoString": "",
                "InternetAccess": 0,
                "type": 0,
                "MACAddr": "",
                "swipeCardType": 0,
                "serialNo": 1,
                "userType": "normal",
                "currentVerifyMode": "invalid",
                "attendanceStatus": "undefined",
                "statusValue": 0,
                "mask": "unknown"
            },
            {
                "major": 5,
                "minor": 22,
                "time": "2021-12-02T10:45:33+07:00",
                "netUser": "",
                "remoteHostAddr": "0.0.0.0",
                "cardNo": "",
                "cardType": 0,
                "name": "",
                "reportChannel": 0,
                "cardReaderKind": 0,
                "cardReaderNo": 0,
                "doorNo": 1,
                "verifyNo": 0,
                "alarmInNo": 0,
                "alarmOutNo": 0,
                "caseSensorNo": 0,
                "RS485No": 0,
                "multiCardGroupNo": 0,
                "deviceNo": 0,
                "employeeNoString": "",
                "InternetAccess": 0,
                "type": 0,
                "MACAddr": "",
                "swipeCardType": 0,
                "serialNo": 2,
                "userType": "normal",
                "currentVerifyMode": "invalid",
                "attendanceStatus": "undefined",
                "statusValue": 0,
                "mask": "unknown"
            },
            {
                "major": 5,
                "minor": 75,
                "time": "2021-12-02T10:53:49+07:00",
                "netUser": "",
                "remoteHostAddr": "0.0.0.0",
                "cardNo": "",
                "cardType": 1,
                "name": "user1",
                "reportChannel": 0,
                "cardReaderKind": 0,
                "cardReaderNo": 1,
                "doorNo": 1,
                "verifyNo": 0,
                "alarmInNo": 0,
                "alarmOutNo": 0,
                "caseSensorNo": 0,
                "RS485No": 0,
                "multiCardGroupNo": 0,
                "deviceNo": 0,
                "employeeNoString": "1",
                "InternetAccess": 0,
                "type": 0,
                "MACAddr": "",
                "swipeCardType": 0,
                "serialNo": 3,
                "userType": "normal",
                "currentVerifyMode": "cardOrFaceOrFp",
                "attendanceStatus": "undefined",
                "statusValue": 0,
                "mask": "no",
                "pictureURL": "http://192.168.1.11:8/LOCALS/pic/acsLinkCap/202112_00/02_105349_30075_0.jpeg@WEB000000000619",
                "picturesNumber": 1
            },
            {
                "major": 5,
                "minor": 21,
                "time": "2021-12-02T10:53:49+07:00",
                "netUser": "",
                "remoteHostAddr": "0.0.0.0",
                "cardNo": "",
                "cardType": 0,
                "name": "",
                "reportChannel": 0,
                "cardReaderKind": 0,
                "cardReaderNo": 0,
                "doorNo": 1,
                "verifyNo": 0,
                "alarmInNo": 0,
                "alarmOutNo": 0,
                "caseSensorNo": 0,
                "RS485No": 0,
                "multiCardGroupNo": 0,
                "deviceNo": 0,
                "employeeNoString": "",
                "InternetAccess": 0,
                "type": 0,
                "MACAddr": "",
                "swipeCardType": 0,
                "serialNo": 4,
                "userType": "normal",
                "currentVerifyMode": "invalid",
                "attendanceStatus": "undefined",
                "statusValue": 0,
                "mask": "unknown"
            },
            {
                "major": 5,
                "minor": 75,
                "time": "2021-12-02T10:53:52+07:00",
                "netUser": "",
                "remoteHostAddr": "0.0.0.0",
                "cardNo": "",
                "cardType": 1,
                "name": "user1",
                "reportChannel": 0,
                "cardReaderKind": 0,
                "cardReaderNo": 1,
                "doorNo": 1,
                "verifyNo": 0,
                "alarmInNo": 0,
                "alarmOutNo": 0,
                "caseSensorNo": 0,
                "RS485No": 0,
                "multiCardGroupNo": 0,
                "deviceNo": 0,
                "employeeNoString": "1",
                "InternetAccess": 0,
                "type": 0,
                "MACAddr": "",
                "swipeCardType": 0,
                "serialNo": 5,
                "userType": "normal",
                "currentVerifyMode": "cardOrFaceOrFp",
                "attendanceStatus": "undefined",
                "statusValue": 0,
                "mask": "no",
                "pictureURL": "http://192.168.1.11:8/LOCALS/pic/acsLinkCap/202112_00/02_105352_30075_0.jpeg@WEB000000000620",
                "picturesNumber": 1
            }
        ]
    }
}

我需要获取时间、姓名、卡号、图片URL

结果是这样的

2021-12-02T10:45:33+07:00 NULL NULL NULL

2021-12-02T10:45:33+07:00 NULL NULL NULL

2021-12-02T10:53:52+07:00 NULL user1 http://192.168.1.11:8/LOCALS/pic/acsLinkCap/202112_00/02_105349_30075_0.jpeg@WEB000000000619

2021-12-02T10:53:49+07:00 NULL NULL NULL

2021-12-02T10:53:52+07:00 NULL user1 http://192.168.1.11:8/LOCALS/pic/acsLinkCap/202112_00/02_105352_30075_0.jpeg@WEB000000000620

我该怎么做。 谢谢


编辑 我可以通过 Dave Cross 修复它。 现在是我的代码。

#!/bin/perl
use JSON;
use feature qw(say);
use strict; 
use warnings;

my $json = '{
    "AcsEvent": {
        "searchID": "1",
        "responseStatusStrg": "MORE",
        "numOfMatches": 5,
        "totalMatches": 13430,
        "InfoList": [
            {
              ....cut off data ......
            }
        ]
    }
}';
my $data = JSON->new->decode($json);
my @infolist = @{ $data->{AcsEvent}{InfoList} };

for my $item (@infolist) {
  say $item->{time};
  say $item->{cardNo};
  say $item->{name};
  say $item->{pictureURL};
}

所以你有一个包含 JSON-encoded 数据结构的文本字符串。首先要做的是将其转换为 Perl 数据结构。我们可以使用 JSON 模块来做到这一点。

use JSON;

# Assuming that you've got your JSON text in $json
my $data = JSON->new->decode($json);

您的 $data 变量现在包含一个散列引用。你可以看出它是一个散列引用,因为 JSON 文本被 { ... }.

包围

您想使用 JSON 的 InfoList 部分。我们可以通过在数据结构中查找几个键来获得它。

my @infolist = @{ $data->{AcsEvent}{InfoList} };

请注意 $data->{AcsEvent}{InfoList} 是一个数组引用(我们可以分辨出来,因为它被 [ ... ] 括起来了)。所以我们可以取消引用并将其存储在数组变量中 (@infolist).

现在,只是一个遍历新数组的例子。数组的每个元素都是一个哈希引用,因此我们可以非常简单地提取我们想要的信息。

for my $item (@infolist) {
  say $item->{time};
}

填写剩余的输出留作 reader 的练习。