提问人:Nick_Sutton 提问时间:7/29/2023 更新时间:7/29/2023 访问量:25
将 Json 文件中的键与 python 中列表中的值进行比较 [已关闭]
Comparing keys in a Json file with values in a List In python [closed]
问:
我需要将此 Json 文件中的标题与列表中的标题进行比较。我的目标是检查哪些标题等于此列表的值,然后打印有关该标题的信息。例如,如果其中一个标题是 Bloons TD 6,我只想打印相应的描述和 viewableDate。
这是我到目前为止所拥有的
response = requests.get("https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions?locale=en-US&country=US&allowCountries=US")
freeGameData = json.loads(response.text)
freeNowList = Epic_Scraper()
#freeNowList returns ['Bloons TD 6', 'Loop Hero']
#freeGameData returns the json listed below
Json 文件如下所示
{
"data": {
"Catalog": {
"searchStore": {
"elements": [
{
"title": "Loop Hero",
"id": "db122f42b41f4dec927e0f280cf653bd",
"namespace": "ff50f85ed609454e80ac46d9496da34d",
"description": "The Lich has thrown the world into a timeless loop and plunged its inhabitants into never ending chaos. Wield an expanding deck of mystical cards to place enemies, buildings, and terrain along each unique expedition loop for the brave hero.",
"effectiveDate": "2021-03-04T18:00:00.000Z",
"offerType": "BASE_GAME",
"expiryDate": null,
"viewableDate": "2021-03-04T18:00:00.000Z",
"status": "ACTIVE",
"isCodeRedemptionOnly": false,
},
{
"title": "Bloons TD 6",
"id": "b27e3b556f1048b9824c7196f32afceb",
"namespace": "6a8dfa6e441e4f2f9048a98776c6077d",
"description": "The Bloons are back and better than ever! Get ready for a massive 3D tower defense game designed to give you hours and hours of the best strategy gaming available.",
"effectiveDate": "2022-07-19T12:00:00.000Z",
"offerType": "BASE_GAME",
"expiryDate": null,
"viewableDate": "2022-04-30T00:00:00.000Z",
"status": "ACTIVE",
"isCodeRedemptionOnly": false,
},
{
"title": "Destiny 2: Bungie 30th Anniversary Pack",
"id": "e7b9e222c7274dd28714aba2e06d2a01",
"namespace": "428115def4ca4deea9d69c99c5a5a99e",
"description": "The 30th Anniversary Pack includes a new Dungeon, Gjallarhorn Exotic Rocket Launcher, new weapons, armor, and much more. ",
"effectiveDate": "2022-08-23T13:00:00.000Z",
"offerType": "DLC",
"expiryDate": null,
"viewableDate": "2022-08-08T15:00:00.000Z",
"status": "ACTIVE",
"isCodeRedemptionOnly": false,
},
},
}
答:
0赞
not_speshal
7/29/2023
#1
循环浏览 json 的最内层列表,仅当标题在列表中时才打印信息:
for e in freeGameData["data"]["Catalog"]["searchStore"]["elements"]:
if e["title"] in freeNowList:
print(e["description"])
print(e["viewableDate"]+"\n")
评论
elements
if something in list: