简体   繁体   中英

Python: Finding element in json obj without iterating

Is it possible to check if particular element exists in json without iterating through it? For example, in the following json data, I want to check if appid with value 4000 exists. I need to process hundreds of similar json data set so it needs to be quick and efficient.

{
    "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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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