json - 一次从 Perl JSON 获取单个键值

标签 json perl curl hash iteration

我有 JSON 代码,我正在使用相同的键名提取,我试图一次从键中提取一个值,并将它们传递给 perl 脚本中的变量(在循环中)但是它一次提取所有值,而不是遍历它们。我想从一个键中提取一个值并将其传递给一个变量,然后再次遍历循环以获得下一个值。 JSON 中的数据量发生变化,因此相同键的数量将会增加。

Perl 脚本片段

#!/usr/bin/perl

use warnings;
use strict;
use JSON::XS;

my $res = "test.json";
my $txt = do {                             
    local $/;                              
    open my $fh, "<", $res or die $!;
    <$fh>;                                 
};

my $json = decode_json($txt);

for my $mdata (@{ $json->{results} }) {
    my $sitedomain = "$mdata->{custom_fields}->{Domain}";
    my $routerip   = "$mdata->{custom_fields}->{RouterIP}";

    #vars
    my $domain  = $sitedomain;
    my $host    = $routerip;

   print $domain;
   print $host;
}

打印 $host 变量

print $host;

192.168.201.1192.168.202.1192.168.203.1

打印 $domain 变量

print $domain;

site1.global.localsite2.global.localsite3.global.local

JSON (test.json)

{
"results": [
    {
            "id": 37,
            "url": "http://global.local/api/dcim/sites/37/",
            "display": "Site 1",
            "name": "Site 1",
            "slug": "site1",
            "custom_fields": {
                "Domain": "site1.global.local",
                "RouterIP": "192.168.201.1"
            }
     },
     {
            "id": 38,
            "url": "http://global.local/api/dcim/sites/38/",
            "display": "Site 2",
            "name": "Site 2",
            "slug": "site2",
            "custom_fields": {
                "Domain": "site2.global.local",
                "RouterIP": "192.168.202.1"
            }
      },
      {
            "id": 39,
            "url": "http://global.local/api/dcim/sites/39/",
            "display": "Site 3",
            "name": "Site 3",
            "slug": "site3",
            "custom_fields": {
                "Domain": "site3.global.local",
                "RouterIP": "192.168.203.1"
            }
      }
   ]
}

最佳答案

如果您将 \n 添加到 print 语句,您的代码会产生预期的结果。您可以利用 say如果不需要格式,则不用打印。

use warnings;
use strict;
use feature 'say';

use JSON::XS;

my $res = "test.json";
my $txt = do {                             
    local $/;                              
    open my $fh, "<", $res or die $!;
    <$fh>;                                 
};

my $json = decode_json($txt);

for my $mdata (@{ $json->{results} }) {
    my $sitedomain = "$mdata->{custom_fields}->{Domain}";
    my $routerip   = "$mdata->{custom_fields}->{RouterIP}";

    #vars
    my $domain  = $sitedomain;
    my $host    = $routerip;

    say "$domain $host";
}

代码可以重写为如下更短的形式

use strict;
use warnings;
use feature 'say';

use JSON;

my $fname = 'router_test.json';
my $txt   = do {                             
    local $/;                              
    open my $fh, "<", $fname or die $!;
    <$fh>;                                 
};

my $json = from_json($txt);

say "$_->{custom_fields}{Domain} $_->{custom_fields}{RouterIP}" for @{$json->{results}};

关于json - 一次从 Perl JSON 获取单个键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71552241/

相关文章:

C# 使用 Newtonsoft.Json 将 JSON 字符串反序列化为对象

perl - 延迟 Perl 中范围更改的代码

perl - Perl中获取两个字符串列表的交集

R&RCurl : Error 54 in libcurl

php从文件中解析ascii文本获取内容

json - Play Framework 和 Scala Json,解析包含 JSArray 和 JSObject 的 json

java - 处理 Java 和 C# REST API 之间的 JSON 互操作

java - 将 JSON 解析为 Map

perl - 在 perl 中解析 yaml --> 代码 : YAML_LOAD_ERR_BAD_MAP_ELEMENT

linux - 如何将 tail -f 输出重定向到 curl(HTTP 流)