gpt4 book ai didi

json - 使用 Perl 遍历 JSON

转载 作者:行者123 更新时间:2023-12-01 06:58:21 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Parsing an array encoded in JSON through perl

(2 个回答)


8年前关闭。




我是 Perl 的新手,想遍历这个 JSON 数据并将其打印到屏幕上。

我怎样才能做到这一点?

$arr = '[{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}]';

最佳答案

使用 JSONJSON::XS将 JSON 解码为 Perl 结构。

简单的例子:

use strict;
use warnings;

use JSON::XS;

my $json = '[{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}]';

my $arrayref = decode_json $json;

foreach my $item( @$arrayref ) {
# fields are in $item->{Year}, $item->{Quarter}, etc.
}

关于json - 使用 Perl 遍历 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com