Python 解析 JSON 计数块
Python parsing JSON count Blocks
我有一个从 Wireshark 导出的 .json 文件,其中包含以下示例:
"_source": {
"layers": {
"frame": {
"frame.encap_type": "1",
"frame.time": "Jan 23, 2018 10:32:28.074649000 Mitteleurop\u00c3\u00a4ische Zeit",
"frame.offset_shift": "0.000000000",
"frame.time_epoch": "1516699948.074649000",
"frame.time_delta": "0.000036000",
"frame.time_delta_displayed": "0.000036000",
"frame.time_relative": "141.761654000",
"frame.number": "18",
"frame.len": "76",
"frame.cap_len": "76",
"frame.marked": "0",
"frame.ignored": "0",
"frame.protocols": "eth:ethertype:pn_rt:pn_dcp"
},
"eth": {
"eth.dst": "60:38:e0:e3:16:05",
"eth.dst_tree": {
"eth.dst_resolved": "BelkinIn_e3:16:05",
"eth.addr": "60:38:e0:e3:16:05",
"eth.addr_resolved": "BelkinIn_e3:16:05",
"eth.lg": "0",
"eth.ig": "0"
},
"eth.src": "00:a0:45:84:3c:9c",
"eth.src_tree": {
"eth.src_resolved": "PhoenixC_84:3c:9c",
"eth.addr": "00:a0:45:84:3c:9c",
"eth.addr_resolved": "PhoenixC_84:3c:9c",
"eth.lg": "0",
"eth.ig": "0"
},
"eth.type": "0x00008892"
},
"pn_rt": {
"pn_rt.frame_id": "65277"
},
"pn_dcp": {
"pn_dcp.service_id": "3",
"pn_dcp.service_type": "1",
"pn_dcp.xid": "0x00000007",
"pn_dcp.reserved16": "0",
"pn_dcp.data_length": "50",
"pn_dcp.block": {
"pn_dcp.option": "2",
"pn_dcp.suboption_device": "2",
"pn_dcp.block_length": "5",
"pn_dcp.block_info": "0",
"pn_dcp.suboption_device_nameofstation": "dut"
},
"pn.padding": "data",
"pn_dcp.block": {
"pn_dcp.option": "2",
"pn_dcp.suboption_device": "3",
"pn_dcp.block_length": "6",
"pn_dcp.block_info": "0",
"pn_dcp.suboption_vendor_id": "0x00000174",
"pn_dcp.suboption_device_id": "0x00001234"
},
"pn_dcp.block": {
"pn_dcp.option": "1",
"pn_dcp.suboption_ip": "1",
"pn_dcp.block_length": "8",
"pn.undecoded": "data",
"pn.undecoded_tree": {
"_ws.expert": {
"pn.undecoded_data": "",
"_ws.expert.message": "Undecoded Data, 8 bytes",
"_ws.expert.severity": "6291456",
"_ws.expert.group": "83886080"
}
}
},
"pn_dcp.block": {
"pn_dcp.option": "1",
"pn_dcp.suboption_ip": "2",
"pn_dcp.block_length": "14",
"pn_dcp.suboption_ip_block_info": "1",
"pn_dcp.subobtion_ip_ip": "192.168.0.50",
"pn_dcp.subobtion_ip_subnetmask": "255.255.255.0",
"pn_dcp.suboption_ip_standard_gateway": "0.0.0.0"
}
}
}
}
我可以通过 Python 找出设置了哪个 dcp_option
以及使用了哪个子选项。但我只得到最后 pn_dcp.block
个偏好。所以我的问题是:是否可以从这本字典中计算 pn_dcp.block
?是否可以读出所有pn_dcp.block
信息?
示例代码如下:
if __name__ == '__main__':
j = None
with open(INFILE, 'r') as f:
j = json.load(f)
for p in j:
r = build_line(p)
def build_line(p):
p = p['_source']['layers']
# DCP ----------------------
dcp = p['pn_dcp']
dcp_id = dcp['pn_dcp.service_id']
dcp_type = dcp['pn_dcp.service_type']
# There is no direct option in Get -------
if not 'pn_dcp.block' in dcp:
dcp_block = dcp
else:
dcp_block = dcp['pn_dcp.block']
dcp_option = dcp_block['pn_dcp.option']
# Differenz options --------------------------------
if dcp_option == '1':
dcp_suboption = dcp_block['pn_dcp.suboption_ip']
elif dcp_option == '2':
dcp_suboption = dcp_block['pn_dcp.suboption_device']
elif dcp_option == '3':
dcp_suboption = dcp_block['pn_dcp.suboption_dhcp']
elif dcp_option == '5':
dcp_suboption = dcp_block['pn_dcp.suboption_control']
elif dcp_option == '6':
dcp_suboption = dcp_block['pn_dcp.suboption_deviceinitiative']
elif dcp_option == '255':
dcp_suboption = dcp_block['pn_dcp.suboption_all']
elif dcp_option == '0':
dcp_suboption = dcp_block['pn_dcp.reserved16']
else:
return 'other' # for no option find
# Format -------------------------------------------------------------
act = "%02x%02x" % (int(dcp_id, 10), int(dcp_type, 10))
option = "%02x%02x" % (int(dcp_option, 10), int(dcp_suboption, 10))
# Options ------------------------------------------------------------------
options = OPTIONS.get(option, 'invalid')
activity = ACTIVITIES.get(act, 'invalid')
Event = activity + options
希望对您有所帮助。我正在从 source
到 layers
再到 pn_dcp
下山。然后我搜索块 pn_dcp.block
并读出 dcp_options
和 dcp_suboptions
.
问题是 pn_dcp.block
是重复键,所以当您将 json 解析为对象或字典时,第二个 pn_dcp.block
将重写第一个。唯一的方法是预先过滤文件,这样这个名字就可以是唯一的。我会做这样的事情:
import re
i = 0
def replace(match):
global i
i += 1
return 'pn_dcp.block%i' % i
with open('data.json') as f:
data = f.read()
formatted = json.loads(re.sub("pn_dcp.block([^_])", replace, data))
然后您可以继续您的代码并使用 i
遍历所有 pn_dcp.block
Is it possible to count the pn_dcp.block from this dictionary?
没有,pn_dcp.block
是本词典中的重复键。
键必须是唯一的,因此它总是被最后一次出现覆盖。
And is it possible to read out all pn_dcp.block information?
有点,你可以在json.load
中添加一个object_pairs_hook
:
pn_dcp_blocks=[]
def saveBlocks(*args):
if args[0][0][0]=="pn_dcp.option":
global pn_dcp_blocks
pn_dcp_blocks.append(args[0])
with open(INFILE, 'r') as f:
j = json.load(f, object_pairs_hook =saveBlocks)
输出如下结构:
pn_dcp_blocks = {list} <class 'list'>:
0 = {list} <class 'list'>:
0 = {tuple} <class 'tuple'>: ('pn_dcp.option', '2')
1 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device', '2')
2 = {tuple} <class 'tuple'>: ('pn_dcp.block_length', '5')
3 = {tuple} <class 'tuple'>: ('pn_dcp.block_info', '0')
4 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device_nameofstation', 'dut')
__len__ = {int} 5
1 = {list} <class 'list'>:
0 = {tuple} <class 'tuple'>: ('pn_dcp.option', '2')
1 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device', '3')
2 = {tuple} <class 'tuple'>: ('pn_dcp.block_length', '6')
3 = {tuple} <class 'tuple'>: ('pn_dcp.block_info', '0')
4 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_vendor_id', '0x00000174')
5 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device_id', '0x00001234')
__len__ = {int} 6
2 = {list} <class 'list'>:
<deleted for readability>
3 = {list} <class 'list'>:
<deleted for readability>
__len__ = {int} 4
请注意:您缺少这些元组中的缩进树
我有一个从 Wireshark 导出的 .json 文件,其中包含以下示例:
"_source": {
"layers": {
"frame": {
"frame.encap_type": "1",
"frame.time": "Jan 23, 2018 10:32:28.074649000 Mitteleurop\u00c3\u00a4ische Zeit",
"frame.offset_shift": "0.000000000",
"frame.time_epoch": "1516699948.074649000",
"frame.time_delta": "0.000036000",
"frame.time_delta_displayed": "0.000036000",
"frame.time_relative": "141.761654000",
"frame.number": "18",
"frame.len": "76",
"frame.cap_len": "76",
"frame.marked": "0",
"frame.ignored": "0",
"frame.protocols": "eth:ethertype:pn_rt:pn_dcp"
},
"eth": {
"eth.dst": "60:38:e0:e3:16:05",
"eth.dst_tree": {
"eth.dst_resolved": "BelkinIn_e3:16:05",
"eth.addr": "60:38:e0:e3:16:05",
"eth.addr_resolved": "BelkinIn_e3:16:05",
"eth.lg": "0",
"eth.ig": "0"
},
"eth.src": "00:a0:45:84:3c:9c",
"eth.src_tree": {
"eth.src_resolved": "PhoenixC_84:3c:9c",
"eth.addr": "00:a0:45:84:3c:9c",
"eth.addr_resolved": "PhoenixC_84:3c:9c",
"eth.lg": "0",
"eth.ig": "0"
},
"eth.type": "0x00008892"
},
"pn_rt": {
"pn_rt.frame_id": "65277"
},
"pn_dcp": {
"pn_dcp.service_id": "3",
"pn_dcp.service_type": "1",
"pn_dcp.xid": "0x00000007",
"pn_dcp.reserved16": "0",
"pn_dcp.data_length": "50",
"pn_dcp.block": {
"pn_dcp.option": "2",
"pn_dcp.suboption_device": "2",
"pn_dcp.block_length": "5",
"pn_dcp.block_info": "0",
"pn_dcp.suboption_device_nameofstation": "dut"
},
"pn.padding": "data",
"pn_dcp.block": {
"pn_dcp.option": "2",
"pn_dcp.suboption_device": "3",
"pn_dcp.block_length": "6",
"pn_dcp.block_info": "0",
"pn_dcp.suboption_vendor_id": "0x00000174",
"pn_dcp.suboption_device_id": "0x00001234"
},
"pn_dcp.block": {
"pn_dcp.option": "1",
"pn_dcp.suboption_ip": "1",
"pn_dcp.block_length": "8",
"pn.undecoded": "data",
"pn.undecoded_tree": {
"_ws.expert": {
"pn.undecoded_data": "",
"_ws.expert.message": "Undecoded Data, 8 bytes",
"_ws.expert.severity": "6291456",
"_ws.expert.group": "83886080"
}
}
},
"pn_dcp.block": {
"pn_dcp.option": "1",
"pn_dcp.suboption_ip": "2",
"pn_dcp.block_length": "14",
"pn_dcp.suboption_ip_block_info": "1",
"pn_dcp.subobtion_ip_ip": "192.168.0.50",
"pn_dcp.subobtion_ip_subnetmask": "255.255.255.0",
"pn_dcp.suboption_ip_standard_gateway": "0.0.0.0"
}
}
}
}
我可以通过 Python 找出设置了哪个 dcp_option
以及使用了哪个子选项。但我只得到最后 pn_dcp.block
个偏好。所以我的问题是:是否可以从这本字典中计算 pn_dcp.block
?是否可以读出所有pn_dcp.block
信息?
示例代码如下:
if __name__ == '__main__':
j = None
with open(INFILE, 'r') as f:
j = json.load(f)
for p in j:
r = build_line(p)
def build_line(p):
p = p['_source']['layers']
# DCP ----------------------
dcp = p['pn_dcp']
dcp_id = dcp['pn_dcp.service_id']
dcp_type = dcp['pn_dcp.service_type']
# There is no direct option in Get -------
if not 'pn_dcp.block' in dcp:
dcp_block = dcp
else:
dcp_block = dcp['pn_dcp.block']
dcp_option = dcp_block['pn_dcp.option']
# Differenz options --------------------------------
if dcp_option == '1':
dcp_suboption = dcp_block['pn_dcp.suboption_ip']
elif dcp_option == '2':
dcp_suboption = dcp_block['pn_dcp.suboption_device']
elif dcp_option == '3':
dcp_suboption = dcp_block['pn_dcp.suboption_dhcp']
elif dcp_option == '5':
dcp_suboption = dcp_block['pn_dcp.suboption_control']
elif dcp_option == '6':
dcp_suboption = dcp_block['pn_dcp.suboption_deviceinitiative']
elif dcp_option == '255':
dcp_suboption = dcp_block['pn_dcp.suboption_all']
elif dcp_option == '0':
dcp_suboption = dcp_block['pn_dcp.reserved16']
else:
return 'other' # for no option find
# Format -------------------------------------------------------------
act = "%02x%02x" % (int(dcp_id, 10), int(dcp_type, 10))
option = "%02x%02x" % (int(dcp_option, 10), int(dcp_suboption, 10))
# Options ------------------------------------------------------------------
options = OPTIONS.get(option, 'invalid')
activity = ACTIVITIES.get(act, 'invalid')
Event = activity + options
希望对您有所帮助。我正在从 source
到 layers
再到 pn_dcp
下山。然后我搜索块 pn_dcp.block
并读出 dcp_options
和 dcp_suboptions
.
问题是 pn_dcp.block
是重复键,所以当您将 json 解析为对象或字典时,第二个 pn_dcp.block
将重写第一个。唯一的方法是预先过滤文件,这样这个名字就可以是唯一的。我会做这样的事情:
import re
i = 0
def replace(match):
global i
i += 1
return 'pn_dcp.block%i' % i
with open('data.json') as f:
data = f.read()
formatted = json.loads(re.sub("pn_dcp.block([^_])", replace, data))
然后您可以继续您的代码并使用 i
遍历所有 pn_dcp.block
Is it possible to count the pn_dcp.block from this dictionary?
没有,pn_dcp.block
是本词典中的重复键。
键必须是唯一的,因此它总是被最后一次出现覆盖。
And is it possible to read out all pn_dcp.block information?
有点,你可以在json.load
中添加一个object_pairs_hook
:
pn_dcp_blocks=[]
def saveBlocks(*args):
if args[0][0][0]=="pn_dcp.option":
global pn_dcp_blocks
pn_dcp_blocks.append(args[0])
with open(INFILE, 'r') as f:
j = json.load(f, object_pairs_hook =saveBlocks)
输出如下结构:
pn_dcp_blocks = {list} <class 'list'>:
0 = {list} <class 'list'>:
0 = {tuple} <class 'tuple'>: ('pn_dcp.option', '2')
1 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device', '2')
2 = {tuple} <class 'tuple'>: ('pn_dcp.block_length', '5')
3 = {tuple} <class 'tuple'>: ('pn_dcp.block_info', '0')
4 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device_nameofstation', 'dut')
__len__ = {int} 5
1 = {list} <class 'list'>:
0 = {tuple} <class 'tuple'>: ('pn_dcp.option', '2')
1 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device', '3')
2 = {tuple} <class 'tuple'>: ('pn_dcp.block_length', '6')
3 = {tuple} <class 'tuple'>: ('pn_dcp.block_info', '0')
4 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_vendor_id', '0x00000174')
5 = {tuple} <class 'tuple'>: ('pn_dcp.suboption_device_id', '0x00001234')
__len__ = {int} 6
2 = {list} <class 'list'>:
<deleted for readability>
3 = {list} <class 'list'>:
<deleted for readability>
__len__ = {int} 4
请注意:您缺少这些元组中的缩进树