我正在使用以下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";
Run Code Online (Sandbox Code Playgroud)
输出来了:
$VAR1 = [
{
'text' => 'text1',
'name' => 'abc'
},
{
'text' => 'text2',
'name' => 'xyz'
}
];
1
Run Code Online (Sandbox Code Playgroud)
Cha*_*ens 22
该decode_json
函数返回一个arrayref,而不是一个列表.您必须取消引用它才能获得列表:
my @decoded_json = @{decode_json($json_text)};
Run Code Online (Sandbox Code Playgroud)
你可能想读perldoc perlreftut
和perldoc perlref