我有2个JSON文件file1.json和file2.json。我想用Perl语言比较其中的内容
file1.json
[{
"id": 12036289,
"name": "DEFAULT ADMIN",
"email": "admin@saurabh.com",
"groups": [{
"id": 12036287,
"name": "Service Admin"
}],
"department": {
"id": 12036288,
"name": "Service Admin"
},
"adminUser": true
}, {
"id": 12046238,
"name": "saurabh",
"email":
"vbdfuyqwvbvvbduiqwvbduyvbqwuidvbq@saurabh.com",
"groups": [{
"id": 12046237,
"name": "uiwgbfuisaufusgafuuuuuuuuuuuuugsdaaaaaaiqd"
}],
"department": {
"id": 12046236,
"name": "uiwvbdusguuuuuuuuuuuuuuugsdaaaaaaiuad2"
},
"adminUser": false
}]
file2.json
[{
"id": 12046238,
"name": "saurabh",
"email":
"vbdfuyqwvbvvbduiqwvbduyvbqwuidvbq@saurabh.com",
"groups": [{
"id": 12046237,
"name": "uiwgbfuisaufusgafuuuuuuuuuuuuugsdaaaaaaiqd"
}],
"department": {
"id": 12046236,
"name": "uiwvbdusguuuuuuuuuuuuuuugsdaaaaaaiuad2"
},
"adminUser": false
}, {
"id": 12036289,
"name": "DEFAULT ADMIN",
"email": "admin@saurabh.com",
"groups": [{
"id": 12036287,
"name": "Service Admin"
}],
"adminUser": true,
"department": {
"id": 12036288,
"name": "Service Admin"
}
}]
这里的内容不是顺序而是相同。我尝试的是解码json文件并使用cmp_deeply()
函数比较数组。但是我无法做到这一点。有人有解决这个问题的方法吗?
EDIT 代码
use strict;
use warnings;
use JSON;
use Test::Deep;
my $file1;
#slurp mode
{
open(FILE,'<','file1.json');
local $/ = undef;
$file1 = <FILE>;
}
my $file2;
#slurp mode
{
open(FILE,'<','file2.json');
local $/ = undef;
$file2 = <FILE>;
}
my @array1 = @{decode_json $file1};
my @array2 = @{decode_json $file2};
print @array1;
print cmp_deeply( @array1, @array2 );
答案 0 :(得分:4)
my @array1 = @{decode_json $file1};
my @array2 = @{decode_json $file2};
print cmp_deeply( @array1, @array2 );
应该是
my @array1 = @{decode_json $file1};
my @array2 = @{decode_json $file2};
print cmp_deeply( \@array1, \@array2 );
最好避免无用的复制。
my $array1 = decode_json $file1;
my $array2 = decode_json $file2;
print cmp_deeply( $array1, $array2 );