@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 腳本運行它而無需任何模塊,通常我們使用郵遞員。當我向我的“沙發”尋求幫助時,他只說:學習編程。是的...
我用正則運算式得到的輸出幾乎就是解決方案。它只是在第一個標簽之后停止標簽,不要在另一個左括號上重復它。
uj5u.com熱心網友回復:
$line eq "{" || "["被解釋為
(($line eq '{') or '[')
(正如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)
uj5u.com熱心網友回復:
使用該YAML模塊,您可以獲得快速的人類可讀格式。我確信還有其他和/或更好的方法來格式化您的文本,但這是一個示例。這里的好處是您可以將 JSON 資料結構(使用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: ''
編輯:使用新的 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: ~
uj5u.com熱心網友回復:
向同事尋求幫助,他只是在 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 檔案,這就是解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/506217.html
