简体   繁体   English

Python:无需迭代即可在json obj中查找元素

[英]Python: Finding element in json obj without iterating

Is it possible to check if particular element exists in json without iterating through it? 是否可以在不迭代的情况下检查json中是否存在特定元素? For example, in the following json data, I want to check if appid with value 4000 exists. 例如,在以下json数据中,我要检查是否存在值4000 appid I need to process hundreds of similar json data set so it needs to be quick and efficient. 我需要处理数百个相似的json数据集,因此它需要快速有效。

{
    "response": {
        "game_count": 62,
        "games": [
            {
                "appid": 10,
                "playtime_forever": 15
            },
            {
                "appid": 20,
                "playtime_forever": 0
            },

            ...

            {
                "appid": 4000,
                "playtime_2weeks": 104,
                "playtime_forever": 21190
            }
        ]
    }
}

The relevant object is contained in an array, so no, it is not possible to find it without iteration. 相关对象包含在数组中,所以不,没有迭代就不可能找到它。 The data could be massaged to use the appid as the key for an object that contains the games as the value, but that requires additional preprocessing. 可以对数据进行按摩,以将appid用作包含游戏作为值的对象的键,但是需要额外的预处理。

However, it could be possible to craft a parser such that the appropriate data can be extracted immediately upon parsing. 但是,可以设计一个解析器,以便在解析时可以立即提取适当的数据。 This would piggyback the iteration within the parser itself instead of being explicit code after the fact. 这将背负解析器本身中的迭代,而不是事实之后的显式代码。 See the object_hook argument of the parser. 请参阅解析器的object_hook参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM