1

我试图获取 facebook 移动帖子。

$VAR1 = {
    "mf_story_key" => "225164133113094",
    "page_id" => "102820022014173",
    "page_insights" => {
         "102820022014173" => {
             "actor_id" => "102820022014173",
             "page_id" => "102820022014173",
             "page_id_type" => "page",
             "post_context" => {
                  "publish_time" => "1641702174",
                  "story_name" => "EntStatusCreationStory"
             },
             "psn" => "EntStatusCreationStory",
             "role" => 1,
         }
    },
    "story_attachment_style" => "album",
};

$publish_time = $VAR1->{page_insights}{102820022014173}{post_context}{publish_time};

如果 102820022014173 是动态值,我如何访问没有具体的 publish_time 值?

4

2 回答 2

2

您需要获取keyspage_insights 哈希,然后遍历它们。

use strict;
use warnings;
use 5.010;

my $post = {
    "mf_story_key" => "225164133113094",
    "page_id" => "102820022014173",
    "page_insights" => {
        "102820022014173" => {
            "actor_id" => "102820022014173",
            "page_id" => "102820022014173",
            "page_id_type" => "page",
            "post_context" => {
                "publish_time" => "1641702174",
                "story_name" => "EntStatusCreationStory"
            },
            "psn" => "EntStatusCreationStory",
            "role" => 1,
        }
    },
    "story_attachment_style" => "album",
};

my $insights = $post->{page_insights};

my @insight_ids = keys %{$insights};

for my $id ( @insight_ids ) {
    say "ID $id was published at ",
        $insights->{$id}{post_context}{publish_time};
}

ID 102820022014173 was published at 1641702174
于 2022-01-09T05:06:08.207 回答
1
for my $page_insight ( values( %{ $VAR1->{page_insights} } ) ) {
   my $publish_time = $page_insight->{post_context}{publish_time};

   ...
}

如果总是只有一个元素,

my $page_insight = ( values( %{ $VAR1->{page_insights} } )[0];
my $publish_time = $page_insight->{post_context}{publish_time};
...

(如果您愿意,可以将这两个语句结合起来。)

于 2022-01-09T07:35:53.553 回答