使用 Perl 解析 JSON

Parse JSON using Perl

我已经编写了一个测试脚本来执行某些功能。脚本按预期工作。目前,运行 脚本所需的参数是使用 Getopt::Long 从命令行传递的。我想将命令行参数移动到 json 文件。端点 ip 仍将作为命令行参数传递。 我希望端点 ip 充当密钥。例如,如果端点是 1.1.1.1,我想获取 client_ip、client_interface_ip、originip、....、[=22 中端点 ID 1.1.1.1 下列出的端口=] 下面提到的配置文件。我该怎么做?

脚本的当前版本:

 use Getopt::Long;

     my ($self) = @_;

        GetOptions (
            "endpoint|e=s"           => $self->{'endpoint'},
            "aggregator|a=s"         => $self->{'aggregator'},
            "port|pt=s"              => $self->{'port'},
            "client|c=s"             => $self->{'client'},
            "client_interface|ci=s"  => $self->{'client_interface'},
            "origin|o=s"             => $self->{'origin'},
            "origin_interface|oi=s"  => $self->{'origin_interface'},
            "interfacename|ot=s"     => $self->{'i1'},
            "interfacename2|it=s"    => $self->{'i2'},
             ) || $self->abort( "Invalid command line options.
                   Valid options are endpoint,aggregator,port,client,client_interface,
                     origin,origin_interface,outertunnel,innertunnel,");

       #Terminate the script execution if the reqd args are not passed
       my @required_args = qw(endpoint aggregator port client client_interface            origin origin_interface 
                              );

       for my $command_line_arguments (@required_args) {
         unless ($self->{$command_line_arguments}) {
          $self->abort('missing required argument ' . $command_line_arguments);
         }
       }

  $self->{'tObj'} =  QA::crypto::tunnels->new
      ('host'=> $self->{'endpoint'})
         or $self->abort('[Could not create a QA::Crypto::tunnels object.]');

json 参数文件:

{
        "Endpoints": [{

                "endpoint": "1.1.1.1",
                "client_ip": "3.4.5.6",
                "client_interface_ip": "10.11.12.14",
                "origin": "a.a.a.a",
                "origin_interface": "15.16.17.18",
                "interfacename": "name",
                "interfacename2": "name1",
                "sl": 19,
                "port": 362
        }, {

                "endpoint": "2.2.2.2",
                "client_ip": "19.20.21.22",
                "client_interface_ip": "23.24.25.26",
                "origin": "1.2.3.4",
                "origin_interface": "5.6.7.8",
                "interfacename": "interface name",
                "interfacename2": "interfacename_2",
                "sl": 19,
                "port": 366
        }]
}




#!/usr/bin/perl

use strict;
use warnings;
use JSON;

my $json;
{
   open my $fh, "<", "cfg.txt"
      or die("Can't open file \"cfg.json\": $!\n");
   local $/;
   $json = <$fh>;
}

my $data = decode_json($json);
$json = JSON->new->utf8->pretty->encode($data);

第一个问题是关于 JSON 文件的合适设计。哈希在这里可以很好地发挥作用,而 arrayref 似乎根本不需要。端点值可以是键,它们的值是具有键相关信息的哈希引用。如果您愿意,也可以将端点值保留在其 hashref 中。

您的 JSON 中的键 "Endpoints" 似乎并没有像现在这样发挥作用。但是如果你确实需要它,也许因为会有其他类型的键,那么你可以为它的值另一个哈希引用,它将包含以端点值作为键的哈希引用。

例如

{ 
   "Endpoints": { 
        "1.1.1.1": { "client_ip": "3.4.5.6",     ... },
        "2.2.2.2": { "client_ip": "19.20.21.22", ... },
        ...
   }, 
   "OtherKeys": { ... }, ...
}

最后的值不应以逗号结尾。参见 JSON format

当你将它引入 Perl 时,你将拥有嵌套的 hashrefs,比如

$data = { 
    Endpoints => { 
         1.1.1.1 => { client_ip => '3.4.5.6',     ... }, 
         2.2.2.2 => { client_ip => '19.20.21.22', ... },
    }, 
    OtherKeys => { ... }, 
};

然后这些值被简单地检索为

my $client_ip = $data->{Endpoints}{'1.1.1.1'}{client_ip};

例如,检索所有端点并为它们列出 client_ip

my @endpoints = keys %{$data->{Endpoints}};
foreach my $ip (@endpoints) {
    say $data->{Endpoints}{$ip}{client_ip};
}

Using References in perlref, and is entirely about it. Also see perlreftut and perldsc

我们可以用Data::Dumper

检查()整个数据结构
use Data::Dumper;
my $data = decode_json($json);
print Dumper($data);

还有许多其他包可用于处理嵌套数据结构。

请注意,JSON 软件包附带 encode_json 和相关方法,因此您可以通过编程方式创建该文件。这可能有助于使其格式正确并在将来进行维护。