【问题标题】:Wrong hash filling to JSON, Perl错误的哈希填充到 JSON,Perl
【发布时间】:2023-03-25 15:23:01
【问题描述】:

所以,我正在制作一个程序,从 PostgreSQL 获取数据作为哈希,它分别使用几个 JSON 作为参数文件和输出数据文件。而且我在获取不应该获取的内容时遇到了一些问题。这里是参数json:

{
    "queries": [
        {
            "table_name" : "t1",
            "subqueries": [
                {
                    "query_id" : "t1_1",
                    "query": [
                        .....some sql query
                    ],
                    "to_hash" : { 
                        "target_by" : "type_id",   // key to index by
                        "keys" : [
                            {
                                "source" : "name", // key in hash from db
                                "target" : "name"  // key in new hash
                            },
                            {
                                "source" : "r",
                                "target" : "r"
                            }
                        ]
                    }
                },
                {
                    "query_id" : "t1_2",
                    "query": [
                        .....some sql query
                    ],
                    "to_hash" : { 
                        "target_by" : "type_id",
                        "keys" : [
                            {
                                "source" : "m",
                                "target" : "m"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

....这是一个 perl 子例程:

my $fname = "query_params.json";

my $q_data_raw;
{
    local $/;
    open(my $fh, "<:encoding(UTF-8)", $fname) or oops("$fname: $!");
    $q_data_raw = <$fh>;
    close($fh);
}
my $q_data = JSON->new->utf8->decode($q_data_raw);
my %result;

sub blabla {

    my $data = shift;
    my($tab, $i) = ($data->{table_name}, 0);

    if ($data->{subqueries} ne "false"){
        my %res_hash;
        my @res_arr;
        my $q_id;
        foreach my $sq (@{$data->{subqueries}}){

            my $query = "";
            $q_id = $sq->{query_id};
            print "\n";
            print "$q_id\n";

            for(@{$sq->{query}}){
                $query .= "$_\n"; 
            }

            my $t_by = $sq->{to_hash}{target_by};
            my $q_hash = $db_connection->prepare($query);           
            $q_hash->execute() or die( "Unable to get: " . $db_connection->errstr);

            while(my $res = $q_hash->fetchrow_hashref()) {
                # print Dumper $res; #print #1
                for(@{$sq->{to_hash}->{keys}}){
                    # print "\nkey:\t" . $_->{target} . "\nvalue:\t".$res->{$_->{source}}; #print #2
                    $res_hash{$q_id}{$res->{$t_by}}{$_->{target}} = $res->{$_->{source}};
                }
                $res_hash{$q_id}{$res->{$t_by}}{__id} = $res->{$t_by};
                # print Dumper %res_hash; #print #3
            }
            push @res_arr, $res_hash{$q_id};
            # print Dumper $res_hash{$q_id}; #print #4
            # print Dumper @res_arr; print #5

            $result{$tab}{$q_id} = \@res_arr;
            $q_hash->finish();
        }
    }
}

for (@{$q_data->{queries}}){ // hash from parameter json
     blabla($_);
}
my $json = JSON->new->pretty->encode(\%result);
# to json file

....这就是我得到的:

{
    "t1" : {
        "t1_1" : [
            {
                //type_id_1_* - from first query
                "type_id_1_1" : {
                    "r" : "4746",
                    "__id" : "type_id_1_1",
                    "name" : "blablabla"
                },
                "type_id_1_2" : {
                    "r" : "7338",
                    "__id" : "type_id_1_2",
                    "name" : "nbmnbcxv"
                },
                ....
            },
            {
                //type_id_2_* - from second query
                "type_id_2_1" : {
                   "m" : "6",
                    "__id" : "type_id_2_1"
                },
                "type_id_2_2" : {
                   "m" : "3",
                   "__id" : "type_id_2_2"
                },
                ............
            }
        ],
        "t1_2" : [
            {
                "type_id_1_1" : {
                   "r" : "4746",
                   "__id" : "type_id_1_1",
                   "name" : "blablabla"
                },
                   "type_id_1_2" : {
                      "r" : "7338",
                      "__id" : "type_id_1_2",
                      "name" : "nbmnbcxv"
                   },
                   ....
            },
            {
                "type_id_2_1" : {
                    "m" : "6",
                    "__id" : "type_id_2_1"
                },
                "type_id_2_2" : {
                   "m" : "3",
                    "__id" : "type_id_2_2"
                },
                ............
            }
        ]
    }
}

不知何故,它从其他子查询中获取查询,这是我不想要的。并且循环似乎没问题,可能。我做错了什么?

【问题讨论】:

  • 您似乎没有在@res_arr 中添加任何内容(所以它应该是空的)? $q_hash-&gt;fetchrow_hashref()的返回值格式是什么?
  • 您的 %result 有一个密钥集 $result{$tab} - 根据您显示的 JSON,它应该是 t1,但它在最终打印中显示为 t2。代码显示您打印%res_hash,但(顶级)键应该是t1_1t1_2。除非我误读了其中的一些内容。最后一次打印显示的是什么数据结构?
  • @zdim 是的,我打错了,刚刚更正了 - 它是 t1 以返回 json,而 t1_1t1_2 实际上在 t1 中。
  • @HåkonHægland 据我所知 - 它是带有 sql 查询参数的哈希对象。在第一个 sql 中,它是 type_idnamer。第二个查询有type_idm
  • @HåkonHægland 更新了错过的信息。当复制代码并从原始源中剪切不相关的部分时,我错过了推入@res_arr,但它存在并且不是空的。用作%result 哈希的哈希数组。

标签: json postgresql perl hash


【解决方案1】:

好吧,好像我在错误级别初始化了 @res_arr@res_hash - 必须在 foreach。输出有点像我需要的。至少我没有重复。

多睡点X(

.... 
my $q_id;
foreach my $sq (@{$data->{subqueries}}){
    my %res_hash;
    my @res_arr;

    my $query = "";
    $q_id = $sq->{query_id};
    print "\n";
    print "$q_id\n";
    ..........
}

【讨论】:

  • 很高兴你找到了它!我会将原因归结为所有嵌套的复杂性。 (当情况变得太糟糕时,通常意味着是时候编写一个类了。另一方面,当然,有时我们必须非常仔细地挖掘细节。)
猜你喜欢
  • 2019-07-22
  • 2013-07-09
  • 2011-05-07
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
相关资源
最近更新 更多