【问题标题】:Json Sort in PerlPerl中的Json排序
【发布时间】:2017-09-19 20:37:39
【问题描述】:

我需要对数组元素中的 json 值进行排序。我需要按 json 字段 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" }

【问题讨论】:

  • 你的例子不是 Perl 代码。
  • 您现在可以检查一下吗?
  • 您知道,订购对象成员是个愚蠢的想法吗?它们被定义为“一组无序的名称/值对”。 json.org/index.html

标签: perl


【解决方案1】:

您当前的样本数据可以使用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;

【讨论】:

    【解决方案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放在你的程序中,声明变量。

    【讨论】:

    • 这比我的回答好,有趣的问题:)
    • 有什么理由在排序子例程之外声明$m$n
    • @DaveCross 没有理由,已更正,现在我在范围内声明变量。因为我们不想在作用域之外使用变量。这是对的吗?谢谢:)
    【解决方案3】:

    我同意 cmets 关于对 json 进行排序的观点,但是为了 perl 预告片。

    可能有一种更优雅的方法,但这会奏效

    #!/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;
    }
    

    【讨论】:

      【解决方案4】:

      话虽如此,对 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 上找到:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-24
        • 2015-05-18
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        • 2015-12-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多