perl JSON 解码没有给出预期的结果
perl JSON decode is not giving expected result
我有以下 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 输入有错误?
请提供您的建议。
JSON 解码器只是 "mapping/pointing" 已经解析的先前值的值。您可以看到您的第一个 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;
为什么您认为它不准确?如果我们查看 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'
}
};
我有以下 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 输入有错误? 请提供您的建议。
JSON 解码器只是 "mapping/pointing" 已经解析的先前值的值。您可以看到您的第一个 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;
为什么您认为它不准确?如果我们查看 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'
}
};