【问题标题】:Perl - Accessing data from decode_json outputPerl - 从 decode_json 输出访问数据
【发布时间】:2019-11-04 12:03:57
【问题描述】:

我为我的脚本提供了一个 JSON 数据文件。然后我使用 decode_json 解码了 JSON 数据...

open my $fh, '<:encoding(UTF-8)', $file ir die;
    my $jsondata = do {local $/; <$fh> };
    my $data = decode_json($jsondata);

    #print Dumper $data

    #I am trying to write a foreach loop in here to pull particular bits of
    #the information out that I want to display (as detailed further below)

close $fh;

Dumper 输出如下所示...

$VAR1 = [
          {
            'DataName' => 'FileOfPetsAcrossTheWorld',
            'Information001' => [
                                  {
                                    'Name' => Steve,
                                    'Sex' => 'Male',
                                    'Age' => 24,
                                    'Animals' => [
                                                 'Dog',
                                                 'Cat',
                                                 'Hamster',
                                                 'Parrot
                                                ],
                                    'Location' => 'London',
                                   },
                                   {
                                    'Name' => Dave,
                                    'Sex' => 'Male',
                                    'Age' => 59,
                                    'Animals' => [
                                                 'Fish',
                                                 'Horse',
                                                 'Budgie',
                                                ],
                                    'Location' => 'Paris',
                                   },
                                   {
                                    'Name' => Sandra,
                                    'Sex' => 'Female',
                                    'Age' => 44,
                                    'Animals' => [
                                                 'Snake',
                                                 'Crocodile',
                                                 'Flamingo',
                                                ],
                                    'Location' => 'Syndey',
                                   }
                                 ]
           }
        ];

我正在尝试使用 foreach 外观从该数据结构中检索输出,以便可以打印输出...

Dataname: FileOfPetsAcrossTheWorld
Name: Steve
Animals: Dog, Cat, Parrot, Hamster
Location: London

Name: Dave
Animals: Fish, Horse, Budgie
Location: Paris

Name: Sandra
Animals: Snake, Crocodile, Flamingo
Location: Sydey

我尝试了各种不同的 foreach 循环和来自在线资源的哈希引用代码 sn-ps(以及一些我以前使用过并工作过的)来迭代并从哈希等中提取数据,但我似乎无法让它工作在这种情况下。在其他错误中,我收到诸如“Not a HASH reference at...”之类的错误。

我应该使用什么正确的方法从这种类型的数据结构中提取这些信息?

【问题讨论】:

  • 您可以发布您拥有的代码以便修复它吗?这不是代码编写服务,展示您尝试过的内容将有助于将答案集中在需要修复的内容上。
  • Parrot 后缺少单引号,名称周围缺少引号。
  • "已经尝试了各种来自在线资源的 ... 代码 sn-ps" -- 改用 perlreftutperldsc
  • 在读取文件时不要使用:encoding(UTF-8) 层——例如使用:raw,或者read_binary from File::Slurperdecode_json 已经从 UTF-8 解码,因为 JSON 文件预计会在其中。
  • 如果它已经从 UTF-8 转换为 perl 的本机编码,则使用 JSON::XS-&gt;new-&gt;decode($jsondata)

标签: json perl hash foreach data-dumper


【解决方案1】:
for my $hash (@$data) {
    say "Dataname: $hash->{DataName}";
    for my $info (@{ $hash->{Information001} }) {
        say "Name: $info->{Name}";
        say 'Animals: ', join ', ', @{ $info->{Animals} };
        say "Location: $info->{Location}";
        say "";
    }
}

史蒂夫的动物顺序不同。悉尼拼写为“悉尼”。

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多