linux – json在perl中解码

前端之家收集整理的这篇文章主要介绍了linux – json在perl中解码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在文件sample.txt中有一个json

我想通过指定解码整个json和打印键值.我的每个代码

#!/usr/bin/perl
use JSON; 
use Data::Dumper; 
use JSON::XS qw( decode_json );

open (han1,"sample.txt") or die "can not read this file ";
@array1 = <han1>; 
$tst =  $array1[0];
$text = decode_json $tst; 
print Dumper($text);

我有一个json名称’messages_ready’的密钥.我想打印’messages_ready’的价值..

我的json如下

[
    {
        "arguments": {},"auto_delete": false,"backing_queue_status": {
            "avg_ack_egress_rate": 55.02128728993393,"avg_ack_ingress_rate": 55.02128728993393,"avg_egress_rate": 55.02128728993393,"avg_ingress_rate": 109.64602476156203,"delta": [
                "delta",0
            ],"len": 6465,"next_seq_id": 7847104,"pending_acks": 4,"persistent_count": 0,"q1": 0,"q2": 0,"q3": 0,"q4": 6465,"ram_ack_count": 4,"ram_msg_count": 6465,"target_ram_count": "infinity"
        },"consumers": 4,"durable": true,"exclusive_consumer_tag": "","memory": 19373224,"message_stats": {
            "ack": 7840491,"ack_details": {
                "rate": 60.4
            },"deliver": 7840497,"deliver_details": {
                "rate": 60.4
            },"deliver_get": 7840498,"deliver_get_details": {
                "rate": 60.4
            },"get": 1,"get_details": {
                "rate": 0.0
            },"publish": 7847260,"publish_details": {
                "rate": 105.4
            },"redeliver": 3,"redeliver_details": {
                "rate": 0.0
            }
        },"messages": 6469,"messages_details": {
            "rate": 74.6
        },"messages_ready": 6465,"messages_ready_details": {
            "rate": 74.6
        },"messages_unacknowledged": 4,"messages_unacknowledged_details": {
            "rate": 0.0
        },"name": "reports","node": "rabbit@ip-10-0-0-105","policy": "","status": "running","vhost": "/"
    },{
        "arguments": {},"backing_queue_status": {
            "avg_ack_egress_rate": 0.0,"avg_ack_ingress_rate": 0.0,"avg_egress_rate": 0.0,"avg_ingress_rate": 0.0,"undefined","undefined"
            ],"len": 1,"next_seq_id": 1,"pending_acks": 0,"q4": 1,"ram_ack_count": 0,"ram_msg_count": 1,"consumers": 0,"idle_since": "2013-12-31 13:03:35","memory": 13760,"message_stats": {
            "publish": 1,"publish_details": {
                "rate": 0.0
            }
        },"messages": 1,"messages_details": {
            "rate": 0.0
        },"messages_ready": 1,"messages_ready_details": {
            "rate": 0.0
        },"messages_unacknowledged": 0,"name": "test","vhost": "/"
    }
]

我该怎么办?请帮帮我…拜托

解决方法

请注意,您不需要在一个脚本中使用JSON和JSON :: XS:如果找到它,JSON将自动使用JSON :: XS.所以使用JSON是一样的,因为使用JSON :: XS,但更便携.
use strict;
use warnings;
use JSON::XS 'decode_json';
use Data::Dumper;

my $data;
{
    local $/ = undef;
    open my $fh,'<','Metadata.txt';
    $data = <$fh>;
    close $fh;
}

my $result = decode_json( $data );

for my $report ( @{$result} ) {
    print $report->{messages_ready},"\n";
}
原文链接:https://www.f2er.com/linux/395238.html

猜你在找的Linux相关文章