将 JSON 字符串保存在动态全局字典中,python

发布于 2025-01-12 12:58:10 字数 407 浏览 3 评论 0原文

我是Python新手。我有多个 JSON 消息通过 MQTT 代理发送到 python 应用程序,具有相同的键但不同的值。我想做的是根据某个值将 JSON 字符串存储在动态全局字典中,并且它将被每个具有相同值的新消息替换。 然后我可以从任何函数获取这些值。

例如:如果我有以下消息,

{'timestamp': 1646682501888, 'description': 'address', 'postal': 'AAA', 'city': 'ABC'} 

我想在第一条记录中保存该消息的记录,其中城市为 ABC,邮政为 AAA,如果城市为“EFG”,邮政为“ZZZ”,则将其存储在第二条记录中记录, 每当有新消息到来时,如果它具有相同的城市和邮政地址,它就会更新完整的消息。我想知道如何从任何函数访问字典中的值。 谢谢

I am new to python. I have multiple JSON messages coming to the python application trough MQTT broker with the same keys but different values. what I am trying to do is to store the JSON string on a dynamic global dictionary according to a certain value and it will be replaced with each new messages has that same value.
then I can reach these values from any functions.

for example: if I have the following message

{'timestamp': 1646682501888, 'description': 'address', 'postal': 'AAA', 'city': 'ABC'} 

I want to save a record of this message when city is ABC and postal is AAA in the first record, and if the city is "EFG" and postal is "ZZZ" store it in a second record,
than whenever a new message is coming it will update the full message if it has the same city and postal. please I want to know how to access values in the dictionary from any function.
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文