使用Perl将json转换为数组

时间:2016-02-29 23:55:21

标签: json perl

我有一大块json,其格式如下:

{"page":{"size":7,"number":1,"totalPages":1,"totalElements":7,"resultSetId":null,"duration":0},"content":[{"id":"787edc99-e94f-4132-b596-d04fc56596f9","name":"Verification","attributes":{"ruleExecutionClass":"VerificationRule"},"userTags":[],"links":[{"rel":"self","href":"/endpoint/787edc99-e94f-4132-b596-d04fc56596f9","id":"787edc99-e94f-...

基本上,size属性(在本例中)告诉我内容部分有7个部分。如何将这个json块转换为Perl中的数组,我可以使用size属性吗?或者是否有更简单的方法,比如使用decode_json()?

这是我到目前为止所做的:

my $resources = get_that_json_chunk();  # function returns exactly the json you see, except all 7 resources in the content section
my @decoded_json = @$resources;

foreach my $resource (@decoded_json) {

我也尝试过这样的事情:

my $deserialize = from_json( $resources );
my @decoded_json = (@{$deserialize});

我想迭代数组并处理数据。我已经尝试了几种不同的方法,因为我读了一些关于阵列引用的内容,但我一直在"不是ARRAY引用"错误和"不能使用字符串(" {" page":{" size":7," number":1 ,"到" ...)作为ARRAY参考,而#34;严格参考"在使用"

1 个答案:

答案 0 :(得分:2)

谢谢Matt Jacob:

my $deserialized = decode_json($resources); 
print "$_->{id}\n" for @{$deserialized->{content}};
相关问题