perl:如何在"root"以外的其他内容上对JSON结构进行排序关键属性 [英] perl: how to sort a JSON structure on something other than "root" key attributes

查看:104
本文介绍了perl:如何在"root"以外的其他内容上对JSON结构进行排序关键属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl: 如何使用JSON :: PP对复杂结构进行排序?

Perl: How can I sort a complex structure using JSON::PP ?

来自JSON文档:

由于排序例程在 JSON :: PP范围,给定的子例程 名称和特殊变量$ a,$ b 将以'JSON :: PP ::'开头.

As the sorting routine runs in the JSON::PP scope, the given subroutine name and the special variables $a, $b will begin 'JSON::PP::'.

这是我的尝试,似乎不起作用

Here is my attempt, does not seem to work

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,mag_sus :

I want to sort by key, then the column_def attribute on the attribute key below "column_def", i.e. density, depth_in_m, mag_sus :

{
    "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."
        }
}

推荐答案

除了通过哈希键进行排序外,我不确定我是否希望您对JSON输出进行排序.如果仅此而已,只需向canonical方法传递一个真实的参数即可.

I'm not certain I understand how you want the JSON output to be sorted -- aside from sorting by hash key. If that's all you want, just pass the canonical method a true argument.

use strict;
use warnings;

use JSON::PP;

# A simple hash-of-hashes for exploration.
my $h = {
    Z => { c => 1, d => 2 },
    A => { a => 3, r => 4 },
    B => { c => 5, x => 6 },
    S => { q => 7, d => 8 },
};

my $js = JSON::PP->new;
$js->canonical(1);

my $output = $js->encode($h);
print $output;

如果确实使用sort_by方法,则在sort块中使用$_没有意义:它将代表什么?从文档中尚不清楚sort_by代码将接收什么参数.像这样使用Data::Dumper:

If you do use the sort_by method, it does not make sense to use $_ within the sort block: what would it represent? It was not clear from the documentation what arguments the sort_by code will receive. Using Data::Dumper like this:

use Data::Dumper qw(Dumper);

my $sorter = sub {
    # See what's going on.
    print "$JSON::PP::a cmp $JSON::PP::b\n";
    print Dumper(\@_, $_);
    <STDIN>;

    # Sort hash keys alphabetically.
    $JSON::PP::a cmp $JSON::PP::b;
};

my $output = $js->sort_by($sorter)->encode($h);

您可以推断出sort_by的工作方式如下:(1)它接收两个参数,即JSON::PP对象和当前正在使用的哈希引用; (2)$JSON::PP::a$JSON::PP::b变量保存要比较的哈希键. 但是请注意,因为哈希引用是从叶子节点向上构建的,所以它引用的是JSON输出.它不引用您的原始数据结构.这似乎使编写比较器的任务变得有些棘手.祝你好运.

You can infer that sort_by works like this: (1) it receives two arguments, the JSON::PP object and the hash ref currently being worked with; and (2) the $JSON::PP::a and $JSON::PP::b variables hold the hash keys being compared. But note that the hash ref refers to the JSON output as it is being built from the leaf nodes upward. It does not refer to your original data structure. This would seem to make the task of writing a comparator a bit trickier. Good luck.

my $sorter = sub {
    my ($json_pp_object, $hash_ref) = @_;

    # Write your own comparator here.
};

my $output = $js->sort_by($sorter)->encode($h);

这篇关于perl:如何在"root"以外的其他内容上对JSON结构进行排序关键属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆