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 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序