json 使用 perl 解析
json parsing using perl
我有一个巨大的 json 文件 ~1000 多行。我想实现两件事。
1) 在 json 文件中,如果 "Id" : "232799"
则将 "s"
分配为 "1861"
我使用 decode_json 完成了此操作。它按预期工作。
2) 将 version
数字加 1 并将文件保存在特定目录中。我希望这发生在一个循环中。
我相信 decode_json 应该用于解析 json object/json 数组。
但是,版本是 file.I 中的第一行我不确定解码是否可以 used.Any 建议?
{
"version": "16300173",
"con": {
"s1": {
"key": false,
"global": {
"cu": [
{
"Id": "232799",
"s": 1861,
"Sps": "xx",
"cId": "xyzzde",
"pat": "-123456789",
"Config": true,
"auth": [
{
"Type": "5",
"dir": "in"
},
{
"Type": "6",
"dir": "out"
}
],
脚本:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
my $json;
{
open my $fh, "<", "/a/etc/cm/nw/st/cfg.json"
or die("Can't open file \"/a/etc/cm/nw/st/cfg.json\": $!\n");
local $/;
$json = <$fh>;
}
my $data = decode_json($json);
for my $customers (@{ $data->{global}{cu} }) {
$customers->{s} = 1861
if $customers->{id} ==237299;
}
$json = JSON->new->utf8->pretty->encode($data);
{
open my $fh, ">" ,"/a/etc/cm/nw/st/cfg.json"
or die("Can't open file \"/a/etc/cm/nw/st/cfg.json\": $!\n");
local $/;
print $fh $json;
}
除了缺少的 {con}{s1}
键外,您还有 id
而不是 Id
此代码似乎有效
#!/usr/bin/perl
use strict;
use warnings;
use v5.14.1; # for autodie
use autodie;
use JSON;
use constant JSON_FILE => '/a/etc/cm/nw/st/cfg.json';
my $data = do {
open my $fh, '<', JSON_FILE;
local $/;
decode_json(<$fh>);
};
my $cu = $data->{con}{s1}{global}{cu};
for my $cust ( @$cu ) {
$cust->{s} = 1861 if $cust->{Id} == 232799;
}
++$data->{version};
{
open my $fh, '>', JSON_FILE;
print $fh JSON->new->utf8->pretty->encode($data);
}
我有一个巨大的 json 文件 ~1000 多行。我想实现两件事。
1) 在 json 文件中,如果 "Id" : "232799"
则将 "s"
分配为 "1861"
我使用 decode_json 完成了此操作。它按预期工作。
2) 将 version
数字加 1 并将文件保存在特定目录中。我希望这发生在一个循环中。
我相信 decode_json 应该用于解析 json object/json 数组。 但是,版本是 file.I 中的第一行我不确定解码是否可以 used.Any 建议?
{
"version": "16300173",
"con": {
"s1": {
"key": false,
"global": {
"cu": [
{
"Id": "232799",
"s": 1861,
"Sps": "xx",
"cId": "xyzzde",
"pat": "-123456789",
"Config": true,
"auth": [
{
"Type": "5",
"dir": "in"
},
{
"Type": "6",
"dir": "out"
}
],
脚本:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
my $json;
{
open my $fh, "<", "/a/etc/cm/nw/st/cfg.json"
or die("Can't open file \"/a/etc/cm/nw/st/cfg.json\": $!\n");
local $/;
$json = <$fh>;
}
my $data = decode_json($json);
for my $customers (@{ $data->{global}{cu} }) {
$customers->{s} = 1861
if $customers->{id} ==237299;
}
$json = JSON->new->utf8->pretty->encode($data);
{
open my $fh, ">" ,"/a/etc/cm/nw/st/cfg.json"
or die("Can't open file \"/a/etc/cm/nw/st/cfg.json\": $!\n");
local $/;
print $fh $json;
}
除了缺少的 {con}{s1}
键外,您还有 id
而不是 Id
此代码似乎有效
#!/usr/bin/perl
use strict;
use warnings;
use v5.14.1; # for autodie
use autodie;
use JSON;
use constant JSON_FILE => '/a/etc/cm/nw/st/cfg.json';
my $data = do {
open my $fh, '<', JSON_FILE;
local $/;
decode_json(<$fh>);
};
my $cu = $data->{con}{s1}{global}{cu};
for my $cust ( @$cu ) {
$cust->{s} = 1861 if $cust->{Id} == 232799;
}
++$data->{version};
{
open my $fh, '>', JSON_FILE;
print $fh JSON->new->utf8->pretty->encode($data);
}