使用Perl循环遍历JSON

前端之家收集整理的这篇文章主要介绍了使用Perl循环遍历JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Parsing an array encoded in JSON through perl                                    2个
我是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","Amount":500}]';

my $arrayref = decode_json $json;

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

猜你在找的Perl相关文章