Json在Perl中排序

时间:2017-09-19 07:58:04

标签: perl

我需要对数组元素中的json值进行排序。我需要按json field id排序。有可能排序吗?

在这种情况下可以请任何人帮忙吗?

请找到以下代码。

$json1 = '{"id":"3","name":"John", "age":31, "city":"New York" }';
$json2 = '{"id":"2","name":"Prem", "age":26, "city":"India" }';
$json3 = '{"id":"4","name":"Mark", "age":27, "city":"USA" }';
$json4 = '{"id":"1","name":"Anto", "age":28, "city":"UK" }';
@array_of_json = ($json1,$json2,$json3,$json4);

按ID排序后,需要像这样输出,

{"id":"1","name":"Anto", "age":28, "city":"UK" }
{"id":"2","name":"Prem", "age":26, "city":"India" }
{"id":"3","name":"John", "age":31, "city":"New York" }
{"id":"4","name":"Mark", "age":27, "city":"USA" }

4 个答案:

答案 0 :(得分:4)

您可以使用sort函数的默认行为对您当前的样本数据进行排序。

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';
use Data::Dumper;

my @array_of_json = (
  '{"id":"3","name":"John", "age":31, "city":"New York" }',
  '{"id":"2","name":"Prem", "age":26, "city":"India" }',
  '{"id":"4","name":"Mark", "age":27, "city":"USA" }',
  '{"id":"1","name":"Anto", "age":28, "city":"UK" }',
);

my @sorted = sort @array_of_json;

say Dumper \@sorted;

输出:

$VAR1 = [
          '{"id":"1","name":"Anto", "age":28, "city":"UK" }',
          '{"id":"2","name":"Prem", "age":26, "city":"India" }',
          '{"id":"3","name":"John", "age":31, "city":"New York" }',
          '{"id":"4","name":"Mark", "age":27, "city":"USA" }'
        ];

但是如果你想要更复杂的东西,我建议解码JSON并对数据结构进行排序。

#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';
use JSON 'decode_json';
use Data::Dumper;

my @array_of_json = map { decode_json $_ }  (
  '{"id":"3","name":"John", "age":31, "city":"New York" }',
  '{"id":"2","name":"Prem", "age":26, "city":"India" }',
  '{"id":"4","name":"Mark", "age":27, "city":"USA" }',
  '{"id":"1","name":"Anto", "age":28, "city":"UK" }',
);

my @sorted = sort { $a->{id} <=> $b->{id } } @array_of_json;

say Dumper \@sorted;

答案 1 :(得分:2)

您可以尝试以下方法,

my @sorted = sort {

    my ($m)=$a=~m/"id":"(\d+)/;  

    my ($n)=$b=~m/"id":"(\d+)/;  

     $m<=>$n                  

    } @array_of_json;

print join"\n",@sorted , "\n";

将ID后面的数字存储到$m$n中并对这些变量进行排序。

然后始终将use warningsuse strict放入您的程序中,声明变量。

答案 2 :(得分:0)

我同意关于排序json的评论,但是为了perl teaser。

可能有更优雅的方式,但这将有效

#!/usr/bin/perl

use strict;
undef $/;

my @array_of_json=('{"id":"3","name":"John", "age":31, "city":"New York" }','{"id":"2","name":"Prem", "age":26, "city":"India" }','{"id":"4","name":"Mark", "age":27, "city":"USA" }','{"id":"1","name":"Anto", "age":28, "city":"UK" }');

for (sort mySort @array_of_json) {
    print "$_\n";
}

sub mySort {
        $a=~/"id":"(\d+)"/;
        my $ida=$1;
        $b=~/"id":"(\d+)"/;
        my $idb=$1;
        return $ida <=> $idb;
}

答案 3 :(得分:0)

话虽如此,对JSON对象成员进行排序是一个愚蠢的想法,这可以做你想要的:

#! /usr/bin/perl

use strict;
use warnings;
use JSON::XS;

my @array_of_json=('{"id":"3", "name":"John", "age":31, "city":"New York"}',
                   '{"id":"2", "name":"Prem", "age":26, "city":"India"}',
                   '{"id":"4", "name":"Mark", "age":27, "city":"USA"}',
                   '{"id":"1", "name":"Anto", "age":28, "city":"UK"}');


my $j = JSON::XS->new->utf8->allow_nonref;

for my $h (sort { $a->{id} <=> $b->{id} } map { $j->decode($_) } @array_of_json)
{
  print
      '{',
      join (',',
            map { $j->encode ($_) . ':' . $j->encode ($h->{$_}) }
            ('id', 'name', 'age', 'city')),
      "}\n";
}

您需要Perl模块JSON::XS来解析和编码JSON数据。使用类似apt-get install libjson-xs-perl的内容安装它。

以上内容可以在SO上找到: