在 Perl 中解码和使用 JSON 数据 [英] Decoding and using JSON data in Perl

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

问题描述

我对访问我解码的一些 JSON 数据的内容感到困惑.这是一个例子

I am confused about accessing the contents of some JSON data that I have decoded. Here is an example

我不明白为什么这个解决方案有效,而我自己的却没有.我的问题在下面重新表述

I don't understand why this solution works and my own does not. My questions are rephrased below

my $json_raw = getJSON(); 
my $content  = decode_json($json_raw);
print Data::Dumper($content);

此时我的JSON数据已经转化成这个

At this point my JSON data has been transformed into this

$VAR1 = { 'items' => [ 1, 2, 3, 4 ] };

我的猜测告诉我,一旦解码,对象将是一个散列,其中一个元素具有键 items 和一个数组引用作为值.

My guess tells me that, once decoded, the object will be a hash with one element that has the key items and an array reference as the value.

$content{'items'}[0]

其中 $content{'items'} 将获得数组引用,而外部 $...[0] 将访问数组中的第一个元素并将其解释为标量.但是,这不起作用.我收到一条错误消息 use of uninitialized value [...]

where $content{'items'} would obtain the array reference, and the outer $...[0] would access the first element in the array and interpret it as a scalar. However this does not work. I get an error message use of uninitialized value [...]

但是,以下确实有效:

$content->{items}[0]

其中 $content->{items} 产生数组引用,[0] 访问该数组的第一个元素.

where $content->{items} yields the array reference and [0] accesses the first element of that array.

问题

  • 为什么 $content{'items'} 不返回数组引用?我什至尝试过 @{content{'items'}},认为一旦我从 content{'items'} 获得值,就需要将其解释为数组.但是,我仍然收到未初始化的数组引用.

  • Why does $content{'items'} not return an array reference? I even tried @{content{'items'}}, thinking that, once I got the value from content{'items'}, it would need to be interpreted as an array. But still, I receive the uninitialized array reference.

如何在不使用箭头运算符的情况下访问数组引用?

How can I access the array reference without using the arrow operator?

推荐答案

初学者对初学者的回答 :) 当然不够专业,但也许对你有帮助.

Beginner's answer to beginner :) Sure not as profesional as should be, but maybe helps you.

use strict;    #use this all times
use warnings;  #this too - helps a lot!
use JSON;

my $json_str = '    { "items" : [ 1, 2, 3, 4 ] }    ';
my $content = decode_json($json_str);

你写道:

我的猜测告诉我,一旦解码,对象将是一个散列一个具有键项和数组引用作为值的元素.

My guess tells me that, once decoded, the object will be a hash with one element that has the key items and an array reference as the value.

是的,它是一个散列,但 decode_json 返回一个引用,在这种情况下,是对散列的引用.(来自文档)

Yes, it is a hash, but the the decode_json returns a reference, in this case, the reference to hash. (from the docs)

需要一个 UTF-8(二进制)字符串并尝试解析它作为 UTF-8 编码的 JSON 文本,返回结果引用.

expects an UTF-8 (binary) string and tries to parse that as an UTF-8 encoded JSON text, returning the resulting reference.

在线

my $content = decode_json($json_str);

您分配给 SCALAR 变量(而不是散列).

you assigning to an SCALAR variable (not to hash).

因为你知道:它是一个参考,你可以做下一个:

Because you know: it is a reference, you can do the next:

printf "reftype:%s\n", ref($content);
#print: reftype:HASH       ^   
           #therefore the  +------- is a SCALAR value containing a reference to hash

它是一个 hashref - 你可以转储所有的键

It is a hashref - you can dump all keys

print "key: $_\n" for keys %{$content}; #or in short %$content
#prints: key: items

你也可以将items"(arrayref)的值赋值给一个标量变量

also you can assing the value of the "items" (arrayref) to an scalar variable

my $aref = $content->{items};   #$hashref->{key}
#or
#my $aref = ${$content}{items}; #$hash{key}

不是

#my $aref = $content{items};    #throws error if "use strict;"
#Global symbol "%content" requires explicit package name at script.pl line 20.

$content{item} 正在从散列 %content 请求一个值,而您从未定义/分配过这样的变量.$content 是一个标量变量而不是散列变量 %content.

The $content{item} is requesting a value from the hash %content and you never defined/assigned such variable. the $content is an scalar variable not hash variable %content.

{
    #in perl 5.20 you can also
    use 5.020;
    use experimental 'postderef';
    print "key-postderef: $_\n" for keys $content->%*;
}

现在更深入 - 到 arrayref - 你可以再次打印出引用类型

Now step deeper - to the arrayref - again you can print out the reference type

printf "reftype:%s\n", ref($aref);
#reftype:ARRAY

打印数组的所有元素

print "arr-item: $_\n" for @{$aref};

但再次不是

#print "$_\n" for @aref;
#dies: Global symbol "@aref" requires explicit package name at script.pl line 37.

{
    #in perl 5.20 you can also
    use 5.020;
    use experimental 'postderef';
    print "aref-postderef: $_\n" for $aref->@*;
}

这是一个简单的规则:

my @arr;               #array variable
my $arr_ref = \@arr;   #scalar - containing a reference to @arr

@{$arr_ref} is the same as @arr 
 ^^^^^^^^^^ - array reference in curly brackets

如果你有一个 $arrayref - 在任何你想使用数组的地方使用 @{$array_ref}.

If you have an $arrayref - use the @{$array_ref} everywhere you want use the array.

my %hash;              #hash variable
my $hash_ref = \%hash; #scalar - containing a reference to %hash

%{$hash_ref} is the same as %hash
 ^^^^^^^^^^^ - hash reference in curly brackets

如果你有一个 $hash_ref - 使用 %{$hash_ref} 任何你想使用哈希的地方.

If you have an $hash_ref - use the %{$hash_ref} everywhere you want use the hash.

对于整个结构,如下

say $content->{items}->[0];
say $content->{items}[0];
say ${$content}{items}->[0];
say ${$content}{items}[0];
say ${$content->{items}}[0];
say ${${$content}{items}}[0];

打印相同的值 1.

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

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