Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 将文件中的数组读入perl数组_Arrays_Json_Perl_File - Fatal编程技术网

Arrays 将文件中的数组读入perl数组

Arrays 将文件中的数组读入perl数组,arrays,json,perl,file,Arrays,Json,Perl,File,我有一个如下所示的文件: { "status": "success", "msg": { "status": "success", "inscount": 2, "critical": 0, "result": [ { "Insulin": "Insuman Rapid", "morning change": 0, "noon change": 1, "

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

{
"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);

if(open(OUTFILE),可能不完全理解这个问题,但我认为以下方法可行:

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

另外,当您可以使用CPAN中的LWP+JSON解析模块时,为什么需要保存到临时文件?@john jensen。因为我正在编辑一个现有的星号IVR代码,这是其他人以前做过的。随着服务的更改,JSON响应将以数组的形式发送结果,在此之前它不在那里,但我想从之前的文件读取此结果事实上他是什么doing@FebinFathah添加解释此代码对我有效