duande8497 2013-07-18 03:48
浏览 88
已采纳

如何在Perl中迭代多维json_decoded对象来创建一个新的json对象?

I'm trying to recreate a php process in Perl and am not having much luck ( I don't know much Perl ).

This is my php code:

$json = '{"0":{"name":"action","value":"none"},"1":{"name":"additionalTraining","value":"modulesRevisited"},"2":{"name":"additionalTrainingComments","value":""},"3":{"name":"additionalTraining","value":"moduleReporting"},"4":{"name":"additionalTrainingComments","value":""},"5":{"name":"additionalTrainingComments","value":""},"6":{"name":"anotherValue","value":""},"7":{"name":"1359436206","value":""},"8":{"name":"1359436207","value":""},"48":{"name":"actionId","value":""},"49":{"name":"multiSelect","value":"second"},"50":{"name":"multiSelect","value":"third"},"51":{"name":"radio","value":"1"},"52":{"name":"svgs","value":{"0":{"id":"drawing"},"1":{"id":"drawing"}}}}';
$decoded = json_decode($json,true);
$clean = array();
foreach($decoded as $Obj => $array){
    if(array_key_exists($array['name'], $clean))
    {
        if(!is_array($clean[$array['name']]))
        {
            $value = $clean[$array['name']];
            $clean[$array['name']] = array($value,$array['value']);
        }
        else
        {
            array_push($clean[$array['name']], $array['value']);
        }
    }
    else
    {
        $clean[$array['name']]=$array['value'];
    }
}
 echo json_encode($clean);

UPDATE: This is what I've tried in Perl and the walls I keep hitting:

use JSON;
use Data::Dumper;
use warnings;
use strict;
my @records = decode_json($json_text); 
foreach my $element (@records)
{
      print "$element
";
}

I get a hashref. So I try changing the first line to:

my @records = @{ decode_json($json_text) }; #dereference function as it returns an arrayref, not a list

Then I get "Not an ARRAY reference" when trying to loop through.

Then I try something like this and get no results or errors:

my $records = decode_json($json_text); 
my $i = 0;
my @records;
foreach my $entry (@{$records[0]}) {
    my %listHash = %{$entry};
    my $key;
    my $value;
    $i++;
    while(($key, $value) = each(%listHash)) {   
        my $key;
        my $value;
        print "${key}_$i, $value
";
    }
}

Then I try and combine bits of all of them to get the output I'm after and the closest I come to is:

my %records = %{ decode_json($json_text) }; 
while ( my ($key, $value) = each %records )
{
  my $records;
  #print "key: $key, value: $records{$key}
";
  while ( my ($key, $value) = each %{$value} )
  {
    print "key: $key, value: $value
";
  }
}

But this gives me output like so:

key: name, value: anotherValue
key: value, value: third
key: name, value: multiSelect
key: value, value: moduleReporting

whereas I need output like

key: anotherValue, value: third
key: multiSelect, value: moduleReporting

Where am I going wrong?

How can I recreate the same result in Perl? Your help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douzai3399 2013-07-18 23:49
    关注

    Well, you aren't really going wrong; it is just the JSON format that is being as stupid as possible. The JSON is a hash of keys (which are sorted, numeric strings) which map to another level of hashes, which have the keys name and value:

    {
      "0": {"name": "someKey",    "value": "someValue"},
      "1": {"name": "anotherKey", "value": "anotherValue"}
    }
    

    (This data should probably be encoded as [{"someKey": "someValue"}, {"anotherKey": "anotherValue"}].)

    So what we'll do is to loop through the numerically sorted keys for the outer hash:

    for my $index (sort { $a <=> $b } keys %records) { ... }
    

    Inside that loop, we do not loop through the keys/values of the hash values. Instead, we just pick the values of the keys name and value:

    my $record_value = $records{$index};
    my ($key, $value) = @$record_value{qw/name value/};   # a "hash slice"
    

    and print them:

    say "key: $key, value: $value";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作