5

如何拆分以下数组,以便我每次只能获得 lon 和 lat 点?在 perl 脚本中?

[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.804376,"lat":12.991606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.894376,"lat":12.999606,"bfg":18000,"xyz":null,"jky":null}]

有什么帮助吗?

4

4 回答 4

7

您可以使用Perl 中的 JSON 库来解析 JSON 字符串。之后,您将拥有一个哈希数组,您可以对其进行迭代并提取纬度/经度值。

于 2012-09-10T08:21:28.457 回答
6

按照j0nes的建议使用JSON库,这是一个单行代码,可以打印并在标准中给出:latlon

perl -MJSON -le '$, = "\t"; map { print $_->{lon}, $_->{lat} } @{JSON->new->decode(<>)}'

输出:

77.594376   12.971606
77.604376   12.980606
77.674376   12.981606
77.684376   12.982606
77.744376   12.983606
77.784376   12.990606
77.804376   12.991606
77.824376   12.995606
77.874376   12.997606
77.894376   12.999606

解释:

  • -MJSON加载包。
  • $,将字段分隔符设置为制表符。
  • map将打印命令映射到解码函数返回的数组引用中的每个散列。
于 2012-09-10T09:37:14.300 回答
2

正如其他人已经说过的那样,使用库来完成这项任务会更好(请参阅下面的代码示例)。如果你像 flesk 那样做,你必须确保,lat总是在lon. 这可能更健壮一点,但请注意,我将数据拆分为无效JSON字符串的块:

#!/usr/bin/env perl

use strict;
use warnings;

# split in (invalid) object parts
my @chunks = split /},/ => <DATA>;

# iterate over chunks
for (@chunks) {

    # extract latitude/longitude
    my $lon = /"lon":([^,]+)/ ? $1 : 'UNKNOWN';
    my $lat = /"lat":([^,]+)/ ? $1 : 'UNKNOWN';

    # print data
    print "lon: $lon, lat: $lat\n";
}

__DATA__
[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.804376,"lat":12.991606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.894376,"lat":12.999606,"bfg":18000,"xyz":null,"jky":null}]

输出:

lon: 77.594376, lat: 12.971606
lon: 77.604376, lat: 12.980606
lon: 77.674376, lat: 12.981606
lon: 77.684376, lat: 12.982606
lon: 77.744376, lat: 12.983606
lon: 77.784376, lat: 12.990606
lon: 77.804376, lat: 12.991606
lon: 77.824376, lat: 12.995606
lon: 77.874376, lat: 12.997606
lon: 77.894376, lat: 12.999606

一个更好、更简单的解决方案将只使用JSON库:

#!/usr/bin/env perl

use strict;
use warnings;
use JSON 'decode_json';

# decode input
my $objects = decode_json <DATA>;

# iterate over objects and print data
printf "lon: %f, lat: %f\n", $_->{lon}, $_->{lat} for @$objects;

__DATA__
[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.804376,"lat":12.991606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.894376,"lat":12.999606,"bfg":18000,"xyz":null,"jky":null}]

输出:同上。

所以,除非您完全知道自己在做什么,否则请不要手动操作!:)

于 2012-09-10T09:25:54.447 回答
1

我也建议使用 JSON 库,但如果它只是一个关闭,你可以做这样的事情:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my @data;

while (<DATA>) {
    if (/"lon":([^,]+),"lat":([^,]+)/) {
        push @data, [$1, $2];
    }
}

print Dumper \@data;

__DATA__
[{"lon":77.594376,"lat":12.971606,"bfg":18000,"xyz":null,"jky":null},{"lon":77.604376,"lat":12.980606,"bfg":18000,"xyz":null,"jky":null},"lon":77.674376,"lat":12.981606,"bfg":18000,"xyz":null,"jky":null},"lon":77.684376,"lat":12.982606,"bfg":18000,"xyz":null,"jky":null},"lon":77.744376,"lat":12.983606,"bfg":18000,"xyz":null,"jky":null},"lon":77.784376,"lat":12.990606,"bfg":18000,"xyz":null,"jky":null},"lon":77.804376,"lat":12.991606,bfg":18000,"xyz":null,"jky":null},"lon":77.824376,"lat":12.995606,"bfg":18000,"xyz":null,"jk":null},{"lon":77.874376,"lat":12.997606,"bfg":18000,"xyz":null,"jky":null},"lon":77.894376,"lat":12999606,"bfg":18000,"xyz":null,"jky":null}]

结果:

$VAR1 = [
      [
        '77.594376',
        '12.971606'
      ],
      [
        '77.674376',
        '12.981606'
      ],
      [
        '77.744376',
        '12.983606'
      ],
      [
        '77.804376',
        '12.991606'
      ],
      [
        '77.874376',
        '12.997606'
      ],
      [
        '77.894376',
        '12.999606'
      ]
    ];
于 2012-09-10T09:12:10.203 回答