【发布时间】:2023-03-10 05:35:01
【问题描述】:
我需要用 JSON 打开文件并读取 2 个变量,然后在我的程序中为 vars 赋值。怎么做?
sub Config {
my $filename = 'perl_config.txt';
my $json_text = do {
open(my $json_fh, "<:encoding(UTF-8)", $filename)
or die("Can't open \$filename\": $!\n");
local $/;
<$json_fh>
};
my $json = JSON->new;
my $data = $json->decode($json_text);
for my $key (sort keys %{$data}) {
print "${key}:"; #how to acces to data by these keys?
print "\n";
}
我的 json 文件看起来:
{
"local_host": "localhost",
"local_port": "6000"
}
【问题讨论】:
标签: json perl dictionary hash