首页 > 解决方案 > 如何遍历多个 json 字典来检查相同类型键的值?

问题描述

我希望能够在多个 json 字典中获取“delta”的值。如果有任何改变,我正在使用来自 kivy 的 JsonStore。当我按下启动 check_streak 函数的按钮时,我得到 NameError: name 'delta' is not defined。这是代码:

class MainApp(App):

    def build(self): # build() returns an instance
        self.store = JsonStore("streak.json") # file that stores the streaks:


        return presentation

    def check_streak(self, instance):

        for item in self.store.find(delta):

            if delta > time.time():
                print("early")

            if delta == time.time():
                print("on time")

            if delta < time.time():
                print("late")

这是单独文件中的 json 字典:

{"first": {"action": "first", "action_num": "1", "seconds": 60, "score": 0, "delta": 1555714261.0438898}, "second": {"action": "second", "action_num": "2", "seconds": 120, "score": 0, "delta": 1555879741.894656}}

我希望能够从文件中的每个对象中获取“delta”的值。我该怎么做?

标签: pythonjsonkivy

解决方案


那么变量delta不存在,因此错误。

大概你的意思是for item in self.store.find("delta"):(以 delta 作为字符串)?


推荐阅读