微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在嵌套字典 Python Odoo 中获取值

如何解决在嵌套字典 Python Odoo 中获取值

我有问题……又来了。这与我之前在 Cron 中的问题有关。我有 JSON 值,我想将它输入到数据库中。我需要帮助来获取这个嵌套字典中的值。请帮忙!

JSON

{'folders': [{'id': 94,'name': 'Retargeting January 2021','totalBlacklisted': 606,'uniqueSubscribers': 19988,'totalSubscribers': 19382},{'id': 90,'name': 'Leads','totalBlacklisted': 0,'uniqueSubscribers': 0,'totalSubscribers': 0},{'id': 84,'name': 'Retargeting Year End','totalBlacklisted': 1367,'uniqueSubscribers': 18847,'totalSubscribers': 17480},{'id': 79,'name': 'CRM Folder','uniqueSubscribers': 3,'totalSubscribers': 3},{'id': 56,'name': 'Curioo P','totalBlacklisted': 282,'uniqueSubscribers': 3279,'totalSubscribers': 2997}]}

Python

res = simplejson.loads(response.text)
self.env['get.folders'].create({
            'id' : self.id,'name': res['name'],'email_blacklist': res['totalBlacklisted'],'email_subscribers': res['totalSubscribers'],'unique_subscribers': res['uniqueSubscribers'],'foldersId': res['id'],})

编辑 最后它起作用了。我试图拼出这些值,但我不知道它是如何工作的。感谢 @Jack Dane 的帮助。

for folder in folders.get("folders"):
            names = folder['name']
            ids = folder['id']
            blacklist = folder['totalBlacklisted']
            subscribe = folder['totalSubscribers']
            unique = folder['uniqueSubscribers']
            self.env['sendinblue.get_folders'].create({
                    # 'id' : folder['id'],'name_folder': names,'email_blacklist': blacklist,'email_subscribers': subscribe,'unique_subscribers': unique,'foldersId': ids,})

解决方法

您可以使用 foreach 循环调用 create 函数来遍历文件夹:

folders = {'folders': [{'id': 94,'name': 'Retargeting January 2021','totalBlacklisted': 606,'uniqueSubscribers': 19988,'totalSubscribers': 19382},{'id': 90,'name': 'Leads','totalBlacklisted': 0,'uniqueSubscribers': 0,'totalSubscribers': 0},{'id': 84,'name': 'Retargeting Year End','totalBlacklisted': 1367,'uniqueSubscribers': 18847,'totalSubscribers': 17480},{'id': 79,'name': 'CRM Folder','uniqueSubscribers': 3,'totalSubscribers': 3},{'id': 56,'name': 'Curioo P','totalBlacklisted': 282,'uniqueSubscribers': 3279,'totalSubscribers': 2997}]}

for folder in folders.get("folders"):
    self.env['get.folders'].create({
            'id' : self.id,'name': folder['name'],'email_blacklist': folder['totalBlacklisted'],'email_subscribers': folder['totalSubscribers'],'unique_subscribers': folder['uniqueSubscribers'],'foldersId': folder['id'],})

就我而言,我使用文件夹作为将作为 JSON 返回的变量。

如果您需要任何说明,请告诉我,

谢谢,

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。