@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在另一个开括号上重复它。
3条答案
按热度按时间jhiyze9q1#
$line eq "{" || "["
被解释为(as Deparse将显示给您)。使用
此外,
$data
是一个标量变量,也就是说,它只包含一个值,对它进行迭代只需要一步。通常,您需要迭代数组:
也可以是数组引用:
或者别的什么
polhcujo2#
使用
YAML
模块,您可以快速获得人类可读的格式。(使用JSON::PP
,一个预先安装的核心Perl模块)转换为Perl数据结构,然后转换为可以轻松打印的YAML结构。我用几行代码和几个模块,使用一些示例JSON数据完成了这项工作。输出量:
EDIT:使用新的json数据,我得到以下输出:
4si2a6ki3#
向一位同事寻求帮助,他在5分钟内就写好了,没有经过深思熟虑。
因此,如果任何人不得不读取和打印出没有任何模块的JSON文件,这里是解决方案。