访问JSON解码为HASH和散列引用数组 [英] Accessing JSON decoded into a HASH and an array of hash references

查看:124
本文介绍了访问JSON解码为HASH和散列引用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下转储器输出有一种方法可以遍历每个散列,仅列出每个 results-> id 记录下的项目。我想能够说出这样的话:



print $ results {1342} {'domain'};



,并将结果返回 testing11.com



我必须首先阅读所有结果数组,然后使用 $ results [$ counter] {id} 访问那里的数据?我不知道如何继续。

  $ VAR1 = {
'end_time'=> 1466017739,
'options'=> {
'hour_offset'=> '00',
'timezone'=> 'America / New_York'
},
'field_headers'=> {
'priority'=> '优先',
'status'=> '状态',
'assignee_external_id'=> 'Assignee external id',
'initialized_assigned_at'=> '最初分配在'
},
'results'=> [
{
'priority'=> '高',
'status'=> 'open',
'domain'=> 'testing11.com',
'generated_timestamp'=> 1546547669,
'id'=> 1342
},
{
'priority'=> '低',
'status'=> 'open',
'domain'=> 'testing22.com',
'generated_timestamp'=> 1464567669,
'id'=> 7062
},
{
'priority'=> '低',
'status'=> 'closed',
'domain'=> 'testing33.com',
'generated_timestamp'=> 1464267669,
'id'=> 432
}]
}


解决方案

您的转储显示包含标量,两个哈希值和数组ref的hashref。 arrayref具有元素的hashrefs。如果你想从中检索特定的元素,你需要知道索引。

  $ top_level-> {results}  - < 0> {domain}; #is'testing11.com'
$ top_level-> {results} - > [0] - > {status}; #是'open'

要迭代,取消引用数组

  foreach我的$ result(@ {$ top_level-> {results}}){
print$ result-> {id} \\\
;
}

或者您可以从所有结果元素的特定键,对于 id

  my @ids = map {$ _-> {id}} @ {$ top_level-> {results}}; 
说@ids;

打印

 
1342 7062 432






请注意,使用包含引用的嵌套结构还使用语法

  $ top_level-> {results} [0] {domain}; #is'testing11.com'

- > 在下标之间是可选的,请参阅在Perlref中使用引用中的规则3.



当哈希键是字符串时,它们应该被引用

  $ top_level-> {'results'} [0] {'domain'}; 

但是,语法快捷方式允许我们省略引号。但是如果在 {} 之内还有其他的内容,它将被解释为表达式并评估。所以如果有任何疑问使用报价。你想要一致的符号。



资源:教程 perlreftut ,参考 perlref 和数据结构cookbook, perldsc






解决方案在 stevieb 的答案中给出,创建反向查找。在这里复制参考

 我的$ results = $ VAR1-> {results}; 

我的%by_ip = map {$ _-> {id} => $ _} @ $ results;

打印$ by_ip {1342} - > {domain} \\\
;


Given the following dumper output is there a way to iterate through each hash to list only the items under each results->id record? I want to be able to say things like:

print $results{1342}{'domain'};

and have the statement return testing11.com as a result.

Would I have to first read through all of results array and then use $results[$counter]{id} to access the data in there ? I'm not sure how to proceed.

$VAR1 = { 
          'end_time' => 1466017739,
          'options' => {
                         'hour_offset' => '00',
                         'timezone' => 'America/New_York'
                       },
          'field_headers' => {
                              'priority' => 'Priority',
                              'status' => 'Status',
                              'assignee_external_id' => 'Assignee external id',
                              'initially_assigned_at' => 'Initially assigned at'
                             },
          'results' => [
                         {
                           'priority' => 'High',
                           'status' => 'Open',
                           'domain' => 'testing11.com',
                           'generated_timestamp' => 1546547669,
                           'id' => 1342
                          },
                         {
                           'priority' => 'Low',
                           'status' => 'Open',
                           'domain' => 'testing22.com',
                           'generated_timestamp' => 1464567669,
                           'id' => 7062
                          },
                         {
                           'priority' => 'Low',
                           'status' => 'Closed',
                           'domain' => 'testing33.com',
                           'generated_timestamp' => 1464267669,
                           'id' => 432
                          }]
      }

解决方案

Your dump shows a hashref containing a scalar, two hashrefs, and an arrayref. The arrayref has hashrefs for elements. If you want to retrieve specific elements from it, you need to know the index.

$top_level->{results}->[0]->{domain};  # is 'testing11.com'
$top_level->{results}->[0]->{status};  # is 'open'

To iterate through it dereference the array

foreach my $result (@{ $top_level->{results} }) {
    print "$result->{id}\n";
}

Or you can just get values from all results elements for a particular key, say for id

my @ids = map { $_->{id} } @{ $top_level->{results} };
say "@ids";

Prints

1342 7062 432


Note that with nested structures, which contain references, you can also use syntax

$top_level->{results}[0]{domain};  # is 'testing11.com'  

The -> is optional between subscripts, see rule 3. in Using References in perlref.

When the hash keys are strings they should be quoted

$top_level->{'results'}[0]{'domain'};

However, a syntax shortcut allows us to omit quotes on barewords. But if there is anything other than a bareword inside {} it will be interpreted as an expression and evaluated. So if in any doubt use quotes. You want consistent notation throughout.

Resources: Tutorial perlreftut, reference perlref and data structures cookbook, perldsc.


A direct solution is given in stevieb's answer, creating a reverse lookup. Copied here for reference

my $results = $VAR1->{results};

my %by_ip = map {$_->{id} => $_} @$results;

print "$by_ip{1342}->{domain}\n";

这篇关于访问JSON解码为HASH和散列引用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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