我无法理解这个 Raku 代码有什么问题。
我想从网站获取 JSON,并从 JSON 数组中的每个项目中打印出一个字段(在这种情况下,是任何 Discourse 论坛中最新主题的标题)。
这是我希望工作的代码,但它失败了:
use HTTP::UserAgent;
use JSON::Tiny;
my $client = HTTP::UserAgent.new;
$client.timeout = 10;
my $url = 'https://meta.discourse.org/latest.json';
my $resp = $client.get($url);
my %data = from-json($resp.content);
# I think the problem starts here.
my @topics = %data<topic_list><topics>;
say @topics.WHAT; #=> (Array)
for @topics -> $topic {
say $topic<fancy_title>;
}
Run Code Online (Sandbox Code Playgroud)
错误消息来自以下say $topic<fancy_title>行:
Type Array does not support associative indexing.
in block <unit> at http-clients/http.raku line 18
Run Code Online (Sandbox Code Playgroud)
我原以为$topic应该写为%topic,因为它是一个哈希数组,但这不起作用:
for …Run Code Online (Sandbox Code Playgroud) raku ×1