如何在 Perl 中循环遍历 JSON 对象?
How to loop through a JSON object in Perl?
我正在尝试使用 perl 创建一个 api,api 用于本机反应,当用户在应用程序上提交表单时,我将获得以下对象,
我是 perl 的新手,我迷失了尝试遍历对象:/
{
checkboxes: [
{
id: "1",
fullname: "Name 1",
color: "red",
res: false
},
{
color: "green",
fullname: "Name 2",
id: "2",
res: false
},
{
color: "blue",
id: "3",
fullname: "Name 3",
res: false
}
]
}
我的$data_decoded = decode_json($data);
我正在尝试这个循环,但它只打印了完整的对象。
foreach $a (@data) {
print "value of a: $a\n";
}
以下演示代码演示了循环遍历这些特定数据
use strict;
use warnings;
use feature 'say';
use JSON;
use Data::Dumper;
my $input = do { local $/; <DATA> };
my $data = from_json($input);
say Dumper($data);
for my $obj ( @{$data->{checkboxes}} ) {
say join(",\t", $obj->@{qw/id fullname color/});
}
__DATA__
{
"checkboxes": [
{
"id": "1",
"fullname": "Name 1",
"color": "red",
"res": false
},
{
"color": "green",
"fullname": "Name 2",
"id": "2",
"res": false
},
{
"color": "blue",
"id": "3",
"fullname": "Name 3",
"res": false
}
]
}
输出
$VAR1 = {
'checkboxes' => [
{
'res' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'color' => 'red',
'fullname' => 'Name 1',
'id' => '1'
},
{
'fullname' => 'Name 2',
'color' => 'green',
'res' => $VAR1->{'checkboxes'}[0]{'res'},
'id' => '2'
},
{
'id' => '3',
'color' => 'blue',
'res' => $VAR1->{'checkboxes'}[0]{'res'},
'fullname' => 'Name 3'
}
]
};
1, Name 1, red
2, Name 2, green
3, Name 3, blue
你把你的 JSON 变成了带有 decode_json
的参考 Perl 数据结构(来自某处,Mojo::JSON 就是这样一个地方):
use Mojo::JSON qw(decode_json);
my $data = ...;
my $data_decoded = decode_json($data);
现在您必须弄清楚如何访问 $data_decoded
中的任何内容。可以转储看看它的结构
use Mojo::Util qw(dumper);
print dumper( $data_decoded );
您会看到 Perl 结构与 JSON 结构相同。您有一个散列(JSON 的对象),它有一个指向数组的 checkboxes
键。数组元素是散列。
使用 Perl v5.24 的 postfix dereferencing notation,您将获得所有数组元素:
# uglier circumfix notation @{$data_decoded->{checkboxes}}
my @elements = $data_decoded->{checkboxes}->@*;
你可以把它放在一个循环中:
foreach my $hash ( $data_decoded->{checkboxes}->@* ) {
...
}
现在你得到了$hash
中的每个散列元素,你可以用它做任何你想做的事。 你还没有告诉我们的那部分。 :)
Perl Data Structures Cookbook (perldsc)中有很多散列和数组引用的各种组合的生成和迭代的例子。
你说当 res
键的值为真时你想做某事。在这种情况下,您可以使用 next
跳过 res
为假的项目:
foreach my $hash ( $data_decoded->{checkboxes}->@* ) {
next unless $hash->{res};
say "I'm doing something when res is true";
}
我正在尝试使用 perl 创建一个 api,api 用于本机反应,当用户在应用程序上提交表单时,我将获得以下对象, 我是 perl 的新手,我迷失了尝试遍历对象:/
{
checkboxes: [
{
id: "1",
fullname: "Name 1",
color: "red",
res: false
},
{
color: "green",
fullname: "Name 2",
id: "2",
res: false
},
{
color: "blue",
id: "3",
fullname: "Name 3",
res: false
}
]
}
我的$data_decoded = decode_json($data);
我正在尝试这个循环,但它只打印了完整的对象。
foreach $a (@data) {
print "value of a: $a\n";
}
以下演示代码演示了循环遍历这些特定数据
use strict;
use warnings;
use feature 'say';
use JSON;
use Data::Dumper;
my $input = do { local $/; <DATA> };
my $data = from_json($input);
say Dumper($data);
for my $obj ( @{$data->{checkboxes}} ) {
say join(",\t", $obj->@{qw/id fullname color/});
}
__DATA__
{
"checkboxes": [
{
"id": "1",
"fullname": "Name 1",
"color": "red",
"res": false
},
{
"color": "green",
"fullname": "Name 2",
"id": "2",
"res": false
},
{
"color": "blue",
"id": "3",
"fullname": "Name 3",
"res": false
}
]
}
输出
$VAR1 = {
'checkboxes' => [
{
'res' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ),
'color' => 'red',
'fullname' => 'Name 1',
'id' => '1'
},
{
'fullname' => 'Name 2',
'color' => 'green',
'res' => $VAR1->{'checkboxes'}[0]{'res'},
'id' => '2'
},
{
'id' => '3',
'color' => 'blue',
'res' => $VAR1->{'checkboxes'}[0]{'res'},
'fullname' => 'Name 3'
}
]
};
1, Name 1, red
2, Name 2, green
3, Name 3, blue
你把你的 JSON 变成了带有 decode_json
的参考 Perl 数据结构(来自某处,Mojo::JSON 就是这样一个地方):
use Mojo::JSON qw(decode_json);
my $data = ...;
my $data_decoded = decode_json($data);
现在您必须弄清楚如何访问 $data_decoded
中的任何内容。可以转储看看它的结构
use Mojo::Util qw(dumper);
print dumper( $data_decoded );
您会看到 Perl 结构与 JSON 结构相同。您有一个散列(JSON 的对象),它有一个指向数组的 checkboxes
键。数组元素是散列。
使用 Perl v5.24 的 postfix dereferencing notation,您将获得所有数组元素:
# uglier circumfix notation @{$data_decoded->{checkboxes}}
my @elements = $data_decoded->{checkboxes}->@*;
你可以把它放在一个循环中:
foreach my $hash ( $data_decoded->{checkboxes}->@* ) {
...
}
现在你得到了$hash
中的每个散列元素,你可以用它做任何你想做的事。 你还没有告诉我们的那部分。 :)
Perl Data Structures Cookbook (perldsc)中有很多散列和数组引用的各种组合的生成和迭代的例子。
你说当 res
键的值为真时你想做某事。在这种情况下,您可以使用 next
跳过 res
为假的项目:
foreach my $hash ( $data_decoded->{checkboxes}->@* ) {
next unless $hash->{res};
say "I'm doing something when res is true";
}