读取从文件到perl数组的数组

时间:2014-02-16 09:03:27

标签: arrays json perl file

我有一个如下所示的文件:

{
"status": "success",
"msg": {
    "status": "success",
    "inscount": 2,
    "critical": 0,
    "result": [
        {
            "Insulin": "Insuman Rapid",
            "morning change": 0,
            "noon change": 1,
            "evening change": 0,
            "action": 3,
            "change morning from": "22",
            "change noon from": "9",
            "change evening from": "20",
            "change morning to": "22",
            "change noon to": "12",
            "change evening to": "20",
            "change type": "1"
        },
        {
            "Insulin": "Insuman basal",
            "morning change": 0,
            "noon change": 0,
            "evening change": 0,
            "action": null,
            "change morning from": "7",
            "change noon from": "6",
            "change evening from": "8",
            "change morning to": "7",
            "change noon to": "6",
            "change evening to": "8",
            "change type": “1”
        }
    ],
    "balance": "9974"
}

}

这是来自Web服务的JSON响应,我将其保存到临时文件中。文件。 我想将结果数组提取到对象的perl数组中。

它是通过以下服务调用生成的

system("curl -X POST -H '$CONTENT_TYPE'  -d '$ARGS'  -o  $TEMP_FILE  $SERVICE_URL 2>/dev/null");

我正在使用此代码提取状态,关键和计算

    if (open(OUTFILE,"<$TEMP_FILE")) {
    while(<OUTFILE>) {
        chomp;
        if (/status\"?\:\s*\"success\"/) {
            $SUCCESS=1;
            print"Success File open********* Febin :) \n";
        }

        if (/critical\"?\:\s*\"1\"/) {
            $CRITICAL=1;
        }

        if (/change type\"?\:\s*\"(\d+)\"/) {
            $CHANGE_TYPE=$1;
        }

        if (/balance\"?\:\s*\"([^\"]+)\"/) {
            $BALANCE=$1;
        }

        foreach $key (keys %TIME_VALUES) {
            if(/$key\schange\"?\:\s*\"1\"/) {
                $TIME_VALUES{$key}[0] = 1;
            }

            if(/change $key from\"?\:\s*\"([^\"]+)\"/) {
                $TIME_VALUES{$key}[1] = $1;
            }

            if(/change $key to\"?\:\s*\"([^\"]+)\"/) {
                $TIME_VALUES{$key}[2] = $1;
            }
        }
    }
    close(OUTFILE);

有没有办法做到这一点请帮助

  

我是perl脚本新手

1 个答案:

答案 0 :(得分:2)

也许完全不了解这个问题,但恕我直言以下可以工作:

use Modern::Perl;
use JSON::XS;
use Encode;
use File::Slurp;
use Data::Dumper;

my $file = "./data.json";
my $data = decode_json Encode::encode 'utf8', read_file $file, { binmode => ':utf8' } ;

my $res;
$res->{$_} = $data->{msg}->{$_} for( qw(status critical inscount));

say Dumper $res;
输入文件的

产生:

$VAR1 = {
          'critical' => 0,
          'inscount' => 2,
          'status' => 'success'
        };

您应该使用JSON :: XS将JSON解析为内部perl结构,如果您的数据只是ascii,则可以省略encode/utf部分...

编辑 - 提高可读性 - 评论:

use Modern::Perl;  #use some modern perl features, like: say and strict and warnings

                   #"load" some modules:
use JSON::XS;      #for the JSON parsing
use Encode;        #module for encoding from/to different encodings
use File::Slurp;   #module for reading files into a variable
use Data::Dumper;  #module for dumping data structures

my $file = "./data.json";  #the filename, where your "JSON" data is

#read the file content into the variable (read_file - provided by the File::SLurp)
my $json_from_file = read_file $file, { binmode => ':utf8' };

#encode
my $encoded_json = Encode::encode 'utf8', $json_from_file;

#convert JSON to internal perl-data structure
my $perl_data = decode_json $encoded_json;

say "=== the data structure ===";
say Dumper $perl_data;

# copy the needed data
my $status   = $perl_data->{msg}->{status};
my $critical = $perl_data->{msg}->{critical};
my $inscount = $perl_data->{msg}->{inscount};
#this is useless, because you can use the $perl_data->{msg}->{status} directly