我使用以下Perl代码使用
JSON module解析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";
输出来了:
$VAR1 = [
{
'text' => 'text1','name' => 'abc'
},{
'text' => 'text2','name' => 'xyz'
}
];
1
解决方法
decode_json function返回一个arrayref,而不是列表.您必须取消引用它才能获得列表:
my @decoded_json = @{decode_json($json_text)};
您可能需要阅读perldoc perlreftut和perldoc perlref