【问题标题】:How to loop through a JSON object in Perl?如何通过在Perl中JSON对象循环?
【发布时间】:2021-09-01 16:31:54
【问题描述】:

我正在尝试使用 perl 创建一个 api,该 api 用于 react native,当用户在应用程序上提交表单时,我将得到以下对象, 我是 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";
}

【问题讨论】:

  • 你的意思是for (@{ $data->checkboxes })?
  • @ikegami 这就是我感到困惑的地方,因为 $data 是我得到的,我读到我必须使用 'decode_json' 但在那之后我迷失了如何访问信息。跨度>
  • 我的意思是$You mean for (@{ $data_decoded->checkboxes })(我会命名为$data $json。在任何地方使用_decoded 是没有意义的。)
  • 试试for my $checkbox (@{ $data_decoded->checkboxes }) { print "Color: ", $checkbox->{color}, "\n"; }

标签: json perl


【解决方案1】:

以下演示代码演示了遍历这些特定数据

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

【讨论】:

    【解决方案2】:

    您使用 decode_json 将 JSON 转换为引用 Perl 数据结构(来自某个地方,Mojo::JSON 就是这样一个地方):

    use Mojo::JSON qw(decode_json);
    my $data = ...;
    my $data_decoded = decode_json($data);
    

    现在你必须弄清楚如何访问$data_decoded 中的所有内容。你可以通过dump来查看它的结构

    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";
       }
    

    【讨论】:

    • 谢谢你,我想要对数据做的是如果 res 为真,更新一个表。
    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-04-28
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多