将Perl的Data :: Dumper编码为JSON

时间:2016-02-29 04:54:09

标签: arrays json perl

我有一个Perl Data :: Dumper文件(data.txt),其中包含两个需要转换为JSON的数组。

这就是我的data.txt:

$stats = {
  'oldtime' => '04',
  'parsedlines' => 8,
  'day_lines' => [
    undef,
    8
  ],
  'actions' => {
    'microbe' => 1
  },
  'lastnick' => 'Sky',
  'words' => {
    'Sky' => 1,
    'microbe' => 9
  },
  'times' => {
    '04' => 8
  },
  'totallines' => 8,
  'word_times' => {
    'microbe' => [
      9
    ],
    'Sky' => [
      1
    ]
  },
  'lastvisited' => {
    'microbe' => 1,
    'Parts:' => 1,
    'Sky' => 1
  },
  'line_times' => {
    'microbe' => [
      3
    ],
    'Sky' => [
      1
    ]
  },
  'lengths' => {
    'microbe' => 36,
    'Sky' => 11
  },
  'lastnormal' => '[04:35:01] <Sky> ;algkagkaga
',
  'day_times' => [
    undef,
    [
      8,
      0,
      0,
      0
    ]
  ],
  'days' => 1,
  'joins' => {
    'microbe' => 1,
    'Sky' => 1
  },
  'monocount' => 0,
  'lines' => {
    'Sky' => 1,
    'microbe' => 3
  }
};
$lines = {
  'actionlines' => {
    'microbe' => [
      '[04:33:53] * microbe is doing just a test
'
    ]
  },
  'sayings' => {
    'Sky' => [
      ';algkagkaga'
    ],
    'microbe' => [
      'Hello'
    ]
  }
};

你可以看到,有2个数组:$ stats和$ lines 我想用JSON转换它们。
有人在这里(Borodin)带来了一个有效的解决方案,但它只输出第二个数组($ lines)。这是他的解决方案:

use strict;
use warnings qw/ all FATAL /;

use JSON;

print encode_json( do 'data.txt' or die $! ), "\n";  

但这是输出的内容:

{

    "actionlines":{
        "microbe":[
            "[04:33:53] * microbe is doing just a test\n"
        ]
    },
    "sayings":{
        "microbe":[
            "Hello"
        ],
        "Sky":[
            ";algkagkaga"
        ]
    }

}

所以它只输出$ lines数组......知道我能做些什么来让它工作?

2 个答案:

答案 0 :(得分:1)

要分别访问数据文件中的哈希引用,您需要在执行our

之前使用do 声明它们

喜欢这个

use strict;
use warnings 'all';

use JSON;

our ( $stats, $lines );

do 'data.txt' or die $!;

print encode_json( $stats ), "\n";
print encode_json( $lines ), "\n";

输出

{"day_times":[null,[8,0,0,0]],"actions":{"microbe":1},"lines":{"microbe":3,"Sky":1},"days":1,"lastnick":"Sky","oldtime":"04","words":{"Sky":1,"microbe":9},"lastnormal":"[04:35:01] <Sky> ;algkagkaga\n","times":{"04":8},"joins":{"Sky":1,"microbe":1},"totallines":8,"monocount":0,"lastvisited":{"Parts:":1,"microbe":1,"Sky":1},"word_times":{"microbe":[9],"Sky":[1]},"line_times":{"Sky":[1],"microbe":[3]},"parsedlines":8,"lengths":{"Sky":11,"microbe":36},"day_lines":[null,8]}
{"sayings":{"microbe":["Hello"],"Sky":[";algkagkaga"]},"actionlines":{"microbe":["[04:33:53] * microbe is doing just a test\n"]}}

答案 1 :(得分:0)

您没有两个阵列。你有两个哈希。

您的代码调用do并将其返回值作为encode_json()的唯一参数传递。来自perldoc

  

如果文件编译成功,请返回最后评估的表达式的值。

这意味着您将data.txt文件中最后一个表达式的值作为参数传递给encode_json()。该值是分配给$lines的哈希值。

可能的解决方案:

  1. 生成data.txt时,将两个哈希值合并为一个哈希值。

  2. data.txt调用之前执行encode_json()文件,忽略其返回值。然后,通过按包限定名称引用它们,将两个变量组合成一个匿名数组。因为它们的声明前面没有my,并且它们是在main包中隐式声明的,所以包将是main。以下是它的外观:

  3. require('data.txt');
    print encode_json([$main::stats,$main::lines]), "\n";