perl 通过数组循环忽略\n

nfg76nw0  于 2022-11-15  发布在  Perl
关注(0)|答案(3)|浏览(173)

@edit下面是保存为$content的JSON

[
    {
        "date_created": 1234,
        "fingerprint_hash": null,
        "address": "xx:xx:xx:xx:xx:xx",
        "name": null,
        "manufacturer": "xxx",
        "date_updated": 1234,
        "active": true,
        "date_last_active": 1234,
        "mac_group_info": {
            "name": null,
            "id": 0,
            "remarks": null
        },
        "id": 1234,
        "remarks": null,
        "arp_mapping_info": [
            {
                "ip_info": {
                    "date_changed": 1234,
                    "date_last_kerberos_login": null,
                    "dns_name": "xxxx",
                    "id": 6,
                    "remarks": null,
                    "date_created": 1234,
                    "kerberos_user_name": null,
                    "ip_group_info": {
                        "remarks": null,
                        "id": 0,
                        "name": null
                    },
                    "address": "x.x.x.x"
                },
                "interface_info": [
                    {
                        "link": "on",
                        "name": "x",
                        "discovery_info": null,
                        "speed": 1234,
                        "host_info": {
                            "backup_sensor_id": 0,
                            "type": "xxxx",
                            "radius_coa_flags": null,
                            "moment": 1,
                            "name": null,
                            "host_group_info": {
                                "name": null,
                                "id": 0,
                                "remarks": null
                            },
                            "interface_mib": "D",
                            "uplink_count": 1,
                            "sensor_info": {
                                "id": 1,
                                "os_version": "xxxx",
                                "architecture": "xxxx",
                                "remarks": null,
                                "last_contact": "xxxx",
                                "queue": 0,
                                "address": "x.x.xx",
                                "status": "active",
                                "os_name": "linux",
                                "name": "xxxx",
                                "version": "x.x.x-x"
                            },
                            "mode": "xx",
                            "address": "x.x.x.x",
                            "radius_secret": null,
                            "arp_mib": "2",
                            "cam_count": 1,
                            "discovery2_mib": "C",
                            "discovery1_mib": "3",
                            "radius_coa_port": null,
                            "radius_requests_count": 0,
                            "vlan_count": 1,
                            "access_to_port": 1,
                            "vlan_mib": "C",
                            "interval": 1,
                            "snmp_traps_community": null,
                            "engine_mode": "dynamic",
                            "manufacturer": "ciscoSystems",
                            "engine_id": null,
                            "snmp_traps_version": "1",
                            "mib_options": null,
                            "remarks": null,
                            "snmp_write_version": "2c",
                            "snmp_write_community": "private",
                            "access_to_cam": 1,
                            "status": "ok",
                            "access_to_cto": 1,
                            "access_to_interface": 1,
                            "snmp_read_version": "1",
                            "snmp_read_community": "public",
                            "id": 1,
                            "cam_mib": "C",
                            "arp_count": 1,
                            "access_to_arp": 1,
                            "port_count": 1,
                            "main_sensor_id": 1
                        },
                        "status": "on",
                        "index": 1,
                        "protocols": [
                            "ARP",
                            "NDP"
                        ],
                        "id": 1,
                        "remarks": null,
                        "sniffer_mode": null
                    }
                ],
                "host_id": 1
            }
        ],
        "cam_mapping_info": []
    }
]

这是我的代码:

foreach ($content){
    $content =~ s/\[/\[\n\t/g;
    $content =~ s/{/{\n\t/g;
    $content =~ s/\]/\n\]/g;
    $content =~ s/}/}\n/g;
    $content =~ s/,/\n\t/g;
print $content;
}

Todo:得到和上面JSON一样的输出,但是用一个perl脚本运行它而不使用任何模块,通常我们用 Postman 来做它。当我向我的“沙发”寻求帮助时,他只说:只是学习编程,是的
我用正则表达式得到的输出几乎就是解决方案。它只是在第一个标签后停止塔宾,dent在另一个开括号上重复它。

jhiyze9q

jhiyze9q1#

$line eq "{" || "["被解释为

(($line eq '{') or '[')

(as Deparse将显示给您)。使用

$line eq '{' || $line eq '['

此外,$data是一个标量变量,也就是说,它只包含一个值,对它进行迭代只需要一步。
通常,您需要迭代数组:

foreach my $line (@lines)

也可以是数组引用:

foreach my $line (@$data)

或者别的什么

foreach my $line ($line1, $line2, @rest_of_lines)
# or
foreach my $line (split /\n/, $message)
polhcujo

polhcujo2#

使用YAML模块,您可以快速获得人类可读的格式。(使用JSON::PP,一个预先安装的核心Perl模块)转换为Perl数据结构,然后转换为可以轻松打印的YAML结构。我用几行代码和几个模块,使用一些示例JSON数据完成了这项工作。

use strict;
use warnings;
use JSON::PP;
use YAML;

my $text = do { local $/; <DATA> };
my $json = decode_json($text);
print Dump $json;

__DATA__
{
    "ITEM":[
        {
            "-itemID": "1000000" ,
            "-itemName": "DisneyJuniorLA" ,
            "-thumbUrl": "" ,
            "-packageID": "1" ,
            "-itemPrice": "0" ,
            "-isLock": "true"
        },
        {
            "-itemID": "1000001" ,
            "-itemName": "31 minutos" ,
            "-thumbUrl": "" ,
            "-packageID": "1" ,
            "-itemPrice": "0" ,
            "-isLock": "true"
        },
        {
            "-itemID": "1000002" ,
            "-itemName": "Plaza SÚsamo" ,
            "-thumbUrl": "" ,
            "-packageID": "1" ,
            "-itemPrice": "0" ,
            "-isLock": "true"
        }
    ]
}

输出量:

---
ITEM:
  - -isLock: true
    -itemID: 1000000
    -itemName: DisneyJuniorLA
    -itemPrice: 0
    -packageID: 1
    -thumbUrl: ''
  - -isLock: true
    -itemID: 1000001
    -itemName: 31 minutos
    -itemPrice: 0
    -packageID: 1
    -thumbUrl: ''
  - -isLock: true
    -itemID: 1000002
    -itemName: Plaza S┌samo
    -itemPrice: 0
    -packageID: 1
    -thumbUrl: ''

EDIT:使用新的json数据,我得到以下输出:

- active: !!perl/scalar:JSON::PP::Boolean 1
  address: xx:xx:xx:xx:xx:xx
  arp_mapping_info:
    - host_id: 1
      interface_info:
        - discovery_info: ~
          host_info:
            access_to_arp: 1
            access_to_cam: 1
            access_to_cto: 1
            access_to_interface: 1
            access_to_port: 1
            address: x.x.x.x
            arp_count: 1
            arp_mib: 2
            backup_sensor_id: 0
            cam_count: 1
            cam_mib: C
            discovery1_mib: 3
            discovery2_mib: C
            engine_id: ~
            engine_mode: dynamic
            host_group_info:
              id: 0
              name: ~
              remarks: ~
            id: 1
            interface_mib: D
            interval: 1
            main_sensor_id: 1
            manufacturer: ciscoSystems
            mib_options: ~
            mode: xx
            moment: 1
            name: ~
            port_count: 1
            radius_coa_flags: ~
            radius_coa_port: ~
            radius_requests_count: 0
            radius_secret: ~
            remarks: ~
            sensor_info:
              address: x.x.xx
              architecture: xxxx
              id: 1
              last_contact: xxxx
              name: xxxx
              os_name: linux
              os_version: xxxx
              queue: 0
              remarks: ~
              status: active
              version: x.x.x-x
            snmp_read_community: public
            snmp_read_version: 1
            snmp_traps_community: ~
            snmp_traps_version: 1
            snmp_write_community: private
            snmp_write_version: 2c
            status: ok
            type: xxxx
            uplink_count: 1
            vlan_count: 1
            vlan_mib: C
          id: 1
          index: 1
          link: on
          name: x
          protocols:
            - ARP
            - NDP
          remarks: ~
          sniffer_mode: ~
          speed: 1234
          status: on
      ip_info:
        address: x.x.x.x
        date_changed: 1234
        date_created: 1234
        date_last_kerberos_login: ~
        dns_name: xxxx
        id: 6
        ip_group_info:
          id: 0
          name: ~
          remarks: ~
        kerberos_user_name: ~
        remarks: ~
  cam_mapping_info: []
  date_created: 1234
  date_last_active: 1234
  date_updated: 1234
  fingerprint_hash: ~
  id: 1234
  mac_group_info:
    id: 0
    name: ~
    remarks: ~
  manufacturer: xxx
  name: ~
  remarks: ~
4si2a6ki

4si2a6ki3#

向一位同事寻求帮助,他在5分钟内就写好了,没有经过深思熟虑。

use strict;
use warnings;

my $content = #JSON#
my @arr = split //, $content;
my $length = scalar @arr;
my $tabcounter = 0;
for (my $i=0; $i < $length; $i++) {

   print $arr[$i];
   if ( $arr[$i] eq '[' || $arr[$i] eq ']' || $arr[$i] eq '{' || $arr[$i] eq '}' || $arr[$i] eq ',') {
        print "\n";
        if ( $arr[$i] eq '[' || $arr[$i] eq '{' ) {
            $tabcounter++;
        }
        if ( $arr[$i] eq '}' || $arr[$i] eq ']' ) {
            $tabcounter--;
        }
        for (my $j=0; $j < $tabcounter; $j++) {
            print "\t";
        }
   }
}

因此,如果任何人不得不读取和打印出没有任何模块的JSON文件,这里是解决方案。

相关问题