正在尝试从返回的JSON中检索某些字段:
Event [{'subscriptionId': ['72059810266873859'], 'imdata': [{'fvAEPg': {'attributes': {'annotation': '', 'childAction': '', 'configIssues': '', 'configSt': 'applied', 'descr': '', 'dn': 'uni/tn-Student1/ap-AP_SD1/epg-er34', 'exceptionTag': '', 'extMngdBy': '', 'floodOnEncap': 'disabled', 'fwdCtrl': '', 'hasMcastSource': 'no', 'isAttrBasedEPg': 'no', 'isSharedSrvMsiteEPg': 'no', 'lcOwn': 'local', 'matchT': 'AtleastOne', 'modTs': '2020-05-01T22:31:30.293+01:00', 'monPolDn': 'uni/tn-common/monepg-default', 'name': 'er34', 'nameAlias': '', 'pcEnfPref': 'unenforced', 'pcTag': 'any', 'prefGrMemb': 'exclude', 'prio': 'unspecified', 'rn': '', 'scope': '3047425', 'shutdown': 'no', 'status': 'created', 'triggerSt': 'not_triggerable', 'txId': '6341068275364643083', 'uid': '15374'}}}]}]
使用此函数接收json并使用pyjq:
def printws():
while True:
message = pyjq.all((ws.recv()))
print("Event", message)
wbxt_api.messages.create(room_id, text=(message[0].imdata))
消息按上述方式打印,但我尝试仅获取部分字段并获取此错误:
Traceback (most recent call last):
File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "aci_teste_new161.py", line 180, in printws
wbxt_api.messages.create(room_id, text=(message[0].imdata))
AttributeError: 'dict' object has no attribute 'imdata'
它应该与尝试访问dict值有关,我在python方面相当初级,我似乎无法让它工作
另一种方式:
def printws():
while True:
print((ws.recv()(["imdata"][0]["fvAEPg"]["attributes"]["name"])))
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "aci_teste_new161.py", line 190, in printws
print((ws.recv()(["imdata"][0]["fvAEPg"]["attributes"]["name"])))
TypeError: string indices must be integers
有什么帮助吗
要访问python字典数据,请执行以下操作:
编辑
要在字典中获取未知键,可以执行以下操作:
这将为您提供字典中键的列表。 imdata中只有一个键,因此它将返回给您
现在,如果您想访问
fvAEPg -> attributes -> dn
,可以按照下面的过程进行操作:快乐编码;)
相关问题 更多 >
编程相关推荐