I am using the Following Perl code to parse an array in JSON, using the JSON
module. But the array returned has length 1 and I am not able to iterate over it properly. So the problem is I am not able to use the array returned.
我使用以下Perl代码使用JSON模块解析JSON中的数组。但返回的数组长度为1,我无法正确迭代它。所以问题是我无法使用返回的数组。
#!/usr/bin/perl
use strict;
my $json_text = '[ {"name" : "abc", "text" : "text1"}, {"name" : "xyz", "text" : "text2"} ]';
use JSON;
use Data::Dumper::Names;
my @decoded_json = decode_json($json_text);
print Dumper(@decoded_json), length(@decoded_json), "\n";
The output comes :
输出来了:
$VAR1 = [
{
'text' => 'text1',
'name' => 'abc'
},
{
'text' => 'text2',
'name' => 'xyz'
}
];
1
2 个解决方案
#1
18
The decode_json
function returns an arrayref, not a list. You must dereference it to get the list:
decode_json函数返回一个arrayref,而不是一个列表。您必须取消引用它才能获得列表:
my @decoded_json = @{decode_json($json_text)};
You may want to read perldoc perlreftut
and perldoc perlref
您可能想要阅读perldoc perlreftut和perldoc perlref
#2
2
Regarding JSON, you may want to make sure you install the JSON::XS
module as it is faster and more stable than the pure Perl implementation included with the JSON
module. The JSON
module will use JSON::XS
automatically when it is available.
关于JSON,您可能希望确保安装JSON :: XS模块,因为它比JSON模块附带的纯Perl实现更快,更稳定。 JSON模块在可用时将自动使用JSON :: XS。
#1
18
The decode_json
function returns an arrayref, not a list. You must dereference it to get the list:
decode_json函数返回一个arrayref,而不是一个列表。您必须取消引用它才能获得列表:
my @decoded_json = @{decode_json($json_text)};
You may want to read perldoc perlreftut
and perldoc perlref
您可能想要阅读perldoc perlreftut和perldoc perlref
#2
2
Regarding JSON, you may want to make sure you install the JSON::XS
module as it is faster and more stable than the pure Perl implementation included with the JSON
module. The JSON
module will use JSON::XS
automatically when it is available.
关于JSON,您可能希望确保安装JSON :: XS模块,因为它比JSON模块附带的纯Perl实现更快,更稳定。 JSON模块在可用时将自动使用JSON :: XS。