Search code examples
arraysjsonperlhikvision

Perl External JSON API Hikvision Face scan


Hi please help I try to get AcsEvent from Hikvision Face Scan with perl. Now data return is.

{
    "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
            }
        ]
    }
}

I need to get time,name,cardNo,pictureURL

The result to be like this.

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

How I can do. Thanks


Edit I can fix it by Dave Cross. Here my code now.

#!/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};
}

Solution

  • So you have a text string that contains a JSON-encoded data structure. The first thing to do is to convert that into a Perl data structure. We can use the JSON module to do that.

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

    Your $data variable now contains a hash reference. You can tell it's going to be a hash reference because the JSON text is enclosed by { ... }.

    You want to work with the InfoList section of the JSON. We can get that by looking up a couple of keys in the data structure.

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

    Note that $data->{AcsEvent}{InfoList} is an array reference (we can tell because it's enclosed by [ ... ]). So we can dereference that and store it in an array variable (@infolist).

    Now, it's just a case of walking the new array. Each element of the array is a hash reference so we can extract the information that we want very simply.

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

    Filling in the rest of the output is left as an exercise for the reader.