1

我有以下 JSON 输入 -

{"links":{"self":"/some/path"},"data": [{"type":"some_service","id":"foo","attributes": {"created":true ,"active":true,"suspended":false}}, {"type":"some_service","id":"dummy","attributes":{"created":false}}]}

我正在使用下面的代码 -

use strict;
use warnings;
use JSON::XS;
use Data::Dumper;

my $result = decode_json($input);
print Dumper($result) . "\n";

但我得到低于输出 -

$VAR1 = {
      'data' => [
                  {
                    'attributes' => {
                                      'active' => bless( do{\(my $o = 1)}, 'JSON::XS::Boolean' ),
                                      'created' => $VAR1->{'data'}[0]{'attributes'}{'active'},
                                      'suspended' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' )
                                    },
                    'id' => 'foo',
                    'type' => 'some_service'
                  },
                  {
                    'id' => 'dummy',
                    'attributes' => {
                                      'created' => $VAR1->{'data'}[0]{'attributes'}{'suspended'}
                                    },
                    'type' => 'some_service'
                  }
                ],
      'links' => {
                   'self' => '/some/path'
                 }
    };

看起来 'created' 中的值是 $VAR1->{'data'}[0]{'attributes'}{'active'} 这似乎不准确,并且在其他地方也发生了同样的情况。

我是否缺少代码中的某个地方或 JSON 输入有一些错误?请提供您的建议。

4

2 回答 2

3

JSON 解码器只是将值“映射/指向”到已解析的先前值。你可以看到你的第一created

$VAR1->{'data'}[0]{'attributes'}{'active'},

,其值为true,就像active应该是一样。您正在查看Data::Dumper哈希数组的表示。

如果您要从 Perl 变量中检索一个元素,您会发现它与您的原始输入相匹配:

print $result->{"data"}[0]->{"attributes"}->{"created"}; # prints 1

要在不发生这种情况的情况下打印Data::Dumper输出,只需在脚本中设置此标志:

$Data::Dumper::Deepcopy = 1;

于 2015-08-19T07:00:22.040 回答
0

为什么你认为它不准确?如果我们查看 JSON,active两者created都具有相同的值:true。也许你会发现如下转储结构更清晰:

use JSON::XS     qw( decode_json );
use Data::Dumper qw( );

my $data = decode_json(<<'__EOI__');
{"links":{"self":"/some/path"},"data [{"type":"some_service","id":"foo","attributes": {"created":true,"active":true,"suspended":false}}, {"type":"some_service","id":"dummy","attributes":{"created":false}}]}
__EOI__

print(Data::Dumper->Dump(
   [ JSON::XS::true, JSON::XS::false, $data ],
   [qw( true false data )],
));

输出:

$true = bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' );
$false = bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' );
$data = {
          'data' => [
                      {
                        'attributes' => {
                                          'active' => $true,
                                          'created' => $true,
                                          'suspended' => $false
                                        },
                        'id' => 'foo',
                        'type' => 'some_service'
                      },
                      {
                        'attributes' => {
                                          'created' => $false
                                        },
                        'id' => 'dummy',
                        'type' => 'some_service'
                      }
                    ],
          'links' => {
                       'self' => '/some/path'
                     }
        };
于 2015-08-19T16:16:46.463 回答