Perl: 如何使用JSON::PP对复杂结构进行排序?
来自JSON文档:
由于排序例程在JSON::PP范围内运行,所以给定的子例程名称和特殊变量$a、$b将以'JSON::PP::'开头。
这是我的尝试,似乎不起作用。
open my $fh, ">", $file or warn " exportAsJSON: can't open file: '$file': $!";
print $fh $coder->sort_by(sub {$_->{column_def}->{$JSON::PP::a} cmp $_->{column_def}->{$JSON::PP::b} } )->encode(\%json);
close $fh;
我希望按照关键字排序,然后按照下面"column_def"属性的属性关键字进行排序,即密度、深度和磁性矿物含量:
{
"column_def":
{
"depth_in_m":
{
"names":"depth_in_m",
"pos":"0"
},
"mag_sus":
{
"names":
{
"A_ALIAS":"Mag-Sus.",
"A_DESC":"magnetic susceptibility in SI",
"ATTRIBUTE":"MAG_SUS"
},
"pos":"2"
},
"density":
{
"names":
{
"A_ALIAS":"Density",
"A_DESC":"density in gm\/cc",
"ATTRIBUTE":"DENSITY"
},
"pos":"1"
}
},
"data":
{
"depth_in_m":"14.635",
"mag_sus":"n.a.",
"density":"n.a."
}
}