使用Perl在JSON上循环 [英] loop over JSON using Perl

查看:108
本文介绍了使用Perl在JSON上循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm a newbie to Perl and want to loop over this JSON data and just print it out to the screen.

我该怎么做?

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

推荐答案

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

Use JSON or JSON::XS to decode the JSON into a Perl structure.

简单的例子:

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.
}

这篇关于使用Perl在JSON上循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆