【问题标题】:Flatten out a JSON hash map to an array in perl将 JSON 哈希映射展平为 perl 中的数组
【发布时间】:2017-12-23 23:59:04
【问题描述】:

我是数据结构的新手,对此我有一个疑问,

我有一个 JSON 文件

    {
    "COMP1":[
        {
            "machines"      :   "xyz",
            "logLocation"   :   "a.log",
            "duration"      :   "1h",
            "network"       :   "prefix1",
            "searchString"  :   [
                "ERR",
                "CRIT",
                "WARN"

            ],
            "ignoreSearch"  :   [
                "DEFERRED",
                "ABCCRITXYZ"
            ]
        },
        {
            "machines"      :   "sql2",
            "logLocation"   :   "a.log",
            "duration"      :   "1d",
            "network"       :   "imcr",
            "searchString"  :   [
                "ERR",
                "CRIT",
                "WARN"

            ],
            "ignoreSearch"  :   [
                "DEFERRED",
                "ABCCRITXYZ"
            ]
        }
   ],
    "COMP2":[
        {
            "machines"      :   "sql",
            "logLocation"   :   "a.log",
            "duration"      :   "1h",
            "network"       :   "prefix-1",
            "searchString"  :   [
                "ERR",
                "CRIT",
                "WARN"

            ],
            "ignoreSearch"  :   [
                "DEFERRED",
                "ABCCRITXYZ"
            ]
        }
    ]
}

我想在“searchString”的数组的基础上把这个扁平化成数组

[
   {


 "comp"          : "comp1",
 "machines"      :   "xyz",
 "logLocation"   :   "a.log",
 "duration"      :   "1h",
 "network"       :   "prefix1",
 "searchString"  :    "ERR",
 "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

  {
    "comp"          : "comp1",
    "machines"      :   "xyz",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "prefix1",
    "searchString"  :   "CRIT",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

   {
    "comp"          : "comp1",
    "machines"      :   "xyz",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "prefix1",
    "searchString"  :   "WARN",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    }
],

[
   {
    "comp"          : "comp1",
 "machines"      :   "sql2",
 "logLocation"   :   "a.log",
 "duration"      :   "1h",
 "network"       :   "imcr",
 "searchString"  :    "ERR",
 "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

  {
    "comp"          : "comp1",
    "machines"      :   "sql2",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "imcr",
    "searchString"  :   "CRIT",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    },

   {
    "comp"          : "comp1",
    "machines"      :   "xyz",
    "logLocation"   :   "a.log",
    "duration"      :   "1h",
    "network"       :   "prefix1",
    "searchString"  :   "WARN",
     "ignoreSearch"  : ["DEFERRED","ABCCRITXYZ"]
    }
]

等等, 如何存储它们以便我以后可以单独访问它们? comp2 也是如此。 对不起,我的缩进,我在这里不问那么多。

【问题讨论】:

  • 回复:“我在这里要求的不多”——但在这里你要求很多。看起来您只是在要求某人为您做这件事。您能否展示您所做的事情以及您遇到的困难,以便有人可以帮助您?
  • @zdim 通过该评论,我没有问很多问题,这与我的缩进可能不正确有关。我被困在这一点上,并在这方面寻找一个先机
  • 你需要做研究。这是一个针对您已经编写并遇到特定问题的代码提出特定编程问题的网站。它不是代码编写服务,也不是我们推荐外部资源的地方。请通读help
  • @hitman99 是的,我明白这一点。我的评论使用了这个措辞——让你知道你似乎在要求代码来解决你的问题。 (不知何故,我认为您实际上并没有想到这一点。)至于“先发制人”-我的第二条评论确实回答了您的问题。使用什么数据结构,一个包给你,以及在 SO 上有很多这样的信息。我希望这能给你一个先机。让我们知道如何编码。
  • 文学:教程perlreftut,数据结构食谱perldsc,参考perlref。所以。

标签: arrays json perl hash hashmap


【解决方案1】:
use JSON qw( decode_json encode_json from_json to_json );

sub dclone { from_json(to_json($_[0])) }

my $foos_by_comp = decode_json(...);

my @flattened_foos_grouped_by_comp;
for my $comp (keys(%$foos_by_comp)) {
   my @flattened_foos_of_comp;
   my $foo = $foos_by_comp->{$comp};
   for my $search_string (@{ $foo->{searchString} }) {
       my $flattened_foo = dclone($foo);
       $flattened_foo->{ comp         } = $comp;
       $flattened_foo->{ searchString } = $search_string;
       push @flattened_foos_of_comp, $flattened_foo;
   }

   push @flattened_foos_grouped_by_comp, \@flattened_foos_of_comp;
}

print(encode_json(\@flattened_foos_grouped_by_comp));

【讨论】:

    【解决方案2】:

    我做过类似的事情。欢迎您的 cmets 和建议。 还要感谢 @ikegami 和 @zdim 的帮助。

    use JSON qw( decode_json encode_json from_json to_json );
    
    
    my $component = decode_json($json);
    
    sub dclone { from_json(to_json($_[0])) }
    
    
    my @grouped_by_comp;
    my @testData;
    foreach my $comp (keys %$component){                                     # $comp is an array reference , keys %$component gives an array reference 
        foreach my $test_map (@{$component->{$comp}}){                       #@{$component->{$comp}} is a hash reference in array 
            my $searchStrings = $test_map->{searchString};                      #$test_map->{searchString} is an array reference within a hash 
            foreach my $search ( @$searchStrings ){
                my %tmp_map = %$test_map;
                $tmp_map{component} = $comp;
                $tmp_map{searchString} = $search;
                $tmp_map{ignoreSearch} = [@{$test_map->{ignoreSearch}}];
                push @testData,\%tmp_map;
            }
        }
    }
    print Dumper(\@testData);
    

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 2011-08-11
      • 2011-10-04
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多