解析json的模板.加载消息



我的脚本中有很多类似的代码

sinkId = json_string_into_dict["wirepas"]["packetReceivedEvent"]["header"]["sinkId"]
eventId = json_string_into_dict["wirepas"]["packetReceivedEvent"]["header"]["eventId"]
sourceAddress = json_string_into_dict["wirepas"]["packetReceivedEvent"]["sourceAddress"]
destinationAddress = json_string_into_dict["wirepas"]["packetReceivedEvent"]["destinationAddress"]
sourceEndpoint = json_string_into_dict["wirepas"]["packetReceivedEvent"]["sourceEndpoint"]

我如何定义模板无效重复部分的代码?

我想这样做

PARSING_TEMPLATE = json_string_into_dict["wirepas"]["packetReceivedEvent"]["header"]
PARSING_TEMPLATE + ["sinkId"]
PARSING_TEMPLATE + ["eventId"]
PARSING_TEMPLATE + ["sourceAddress"]
PARSING_TEMPLATE + ["destinationAddress"]
PARSING_TEMPLATE + ["sourceEndpoint"]

您不妨直接引用"packetReceivedEvent"键:

packetReceivedEvent = json_string_into_dict["wirepas"]["packetReceivedEvent"]
sinkId = packetReceivedEvent["header"]["sinkId"]
eventId = packetReceivedEvent["header"]["eventId"]
sourceAddress = packetReceivedEvent["sourceAddress"]
destinationAddress = packetReceivedEvent["destinationAddress"]
sourceEndpoint = packetReceivedEvent["sourceEndpoint"]