You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中高效解析JSON并根据appid查找Steam游戏名称?

Optimizing Steam Game Lookup by AppID & Understanding the JSON Structure

Hey there! Let's tackle your two questions—optimizing the lookup efficiency and breaking down the JSON data structure—step by step.

1. What's the JSON Data Structure?

Your Steam game JSON follows a nested structure of dictionaries (objects) and lists (arrays):

  • The top-level is a dictionary with one key: applist
  • applist maps to another dictionary containing the apps key
  • apps maps to a list of dictionaries, where each dictionary represents a game with two keys: appid (integer) and name (string)

Here's a simplified hierarchy breakdown:

{
  "applist": {  # Dictionary
    "apps": [   # List of game entries
      { "appid": 5, "name": "Dedicated Server" },  # Individual game dictionary
      ...
    ]
  }
}

2. Optimizing the Lookup Efficiency

Your current while/for loop approaches use linear search (O(n) time complexity), which gets slow as the Steam game list grows (it has tens of thousands of entries). The best optimization is to convert the apps list into a lookup dictionary where appid is the key and name is the value—this turns lookups into O(1) (constant time) operations.

Here's the optimized implementation:

import requests

steam_game_db = "YOUR_STEAM_JSON_URL_HERE"
returned_id = "570"  # Example appid string from other queries

# Fetch and parse the JSON data
req_name_from_id = requests.get(steam_game_db)
game_data = req_name_from_id.json()

# Build a reusable lookup dictionary: {appid: game_name}
game_lookup = {app["appid"]: app["name"] for app in game_data["applist"]["apps"]}

# Fast lookup (convert string appid to integer to match the key type)
target_appid = int(returned_id)
returned_game = game_lookup.get(target_appid, "Game not found")
print(returned_game)

Why this works better:

  • Blazing fast lookups: No more looping through every game—just direct key access, even for massive datasets
  • Safer code: Uses get() to handle missing appids gracefully (no crashes from index errors or uncaught conditions)
  • Reusable: Build the lookup dictionary once, then use it for as many appid queries as you need

Issues with your original code:

  • The while loop will crash with an IndexError if the appid doesn't exist (it keeps incrementing i beyond the list length)
  • Both while and for loops waste time with linear search—this adds up quickly when dealing with Steam's full game catalog

内容的提问来源于stack exchange,提问作者Purdy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:51:22