簡體   English   中英

從 JSON 數據訪問特定數據 (Python)

[英]Accessing specific data from JSON data (Python)

I am new to JSON in Python (forgive me if I word something incorrectly) and I am trying to parse JSON information I get from an API. 我成功獲得了 API 信息,但是當我嘗試提取我需要的信息時(特別是變量信息中的“名稱”值)。 我似乎找不到任何有關如何訪問它的信息。 代碼如下:

# Importing JSON handling library for parsing GET request using requests library
import json
import requests
import ast
import collections

# API Endpoint
url = "https://fnbr.co/api/shop"

# API key
key = "" # key is usually here

# Defining Parameters
headers = {"x-api-key":key}

# Sending GET request
req = requests.get(url = url, headers = headers)



info = req.json()
info = info['data']['featured']
print(info)

和 output:

[{'id': '5e8743597576ec53deb18eeb', 'name': 'Deadpool Gear Bundle', 'price': '2,000', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/bundle/5e8743597576ec53deb18eeb/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'bundle', 'slug': 'deadpool-gear-bundle', 'readableType': 'Bundle', 'description': False, 'bundleSet': '5e87444f7576ec029cb18eef', 'bannerText': False, 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8fbb5d3cdac627afdebeb5', 'name': 'Deadpool Mashups', 'price': '2,000', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/bundle/5e8fbb5d3cdac627afdebeb5/icon.png', 'png': False, 'gallery': False, 'featured': False}, 'rarity': 'marvel', 'type': 'bundle', 'slug': 'deadpool-mashups', 'readableType': 'Bundle', 'description': 'Outfit Bundle.', 'bundleSet': '5e8fba7a3cdac6863bdebeaf', 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8742117576ec88f6b18ee0', 'name': 'Chimichanga!', 'price': '300', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/emote/5e8742117576ec88f6b18ee0/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'emote', 'slug': 'chimichanga', 'readableType': 'Emote', 'description': 'Too hot! Too hot!', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8741f37576ecfc79b18edd', 'name': "Scootin'", 'price': '500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/emote/5e8741f37576ecfc79b18edd/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'emote', 'slug': 'scootin', 'readableType': 'Emote', 'description': 'Dangerously dainty.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e87422b7576ec21f5b18ee3', 'name': 'Dragacorn', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/glider/5e87422b7576ec21f5b18ee3/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'glider', 'slug': 'dragacorn', 'readableType': 'Glider', 'description': 'Bask in its mutant rainbow glory.', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8fbafd3cdac6ed02debeb3', 'name': 'Cuddlepool', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5e8fbafd3cdac6ed02debeb3/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5e8fbafd3cdac6ed02debeb3/featured.png', 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'outfit', 'slug': 'cuddlepool', 'readableType': 'Outfit', 'description': 'Ferociously furry.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8fb9f33cdac69b0fdebead', 'name': 'Ravenpool', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5e8fb9f33cdac69b0fdebead/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5e8fb9f33cdac69b0fdebead/featured.png', 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'outfit', 'slug': 'ravenpool', 'readableType': 'Outfit', 'description': 'Maximum darkness.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8741d67576ecad85b18eda', 'name': 'Meaty Mallets', 'price': '800', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/pickaxe/5e8741d67576ecad85b18eda/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'pickaxe', 'slug': 'meaty-mallets', 'readableType': 'Pickaxe', 'description': "Give 'em a thwack.", 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5db7ff1ac02d0515d20b054c', 'name': 'Riley', 'price': '1,200', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5db7ff1ac02d0515d20b054c/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5db7ff1ac02d0515d20b054c/featured.png', 'resizeAvailable': True}, 'rarity': 'rare', 'type': 'outfit', 'slug': 'riley', 'readableType': 'Outfit', 'description': 'The city is her playground.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 5, 'firstSeen': '2019-11-27T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2019-11-27T00:00:00.000Z', '2020-03-09T00:00:00.000Z', '2020-01-01T00:00:00.000Z', '2020-02-06T00:00:00.575Z']}}, {'id': '5db804ebc02d05322e0b059d', 'name': 'Wild X', 'price': '300', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/wrap/5db804ebc02d05322e0b059d/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/wrap/5db804ebc02d05322e0b059d/featured.png'}, 'rarity': 'uncommon', 'type': 'wrap', 'slug': 'wild-x', 'readableType': 'Wrap', 'description': 'Show your style.', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 5, 'firstSeen': '2019-11-27T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2019-11-27T00:00:00.000Z', '2020-03-09T00:00:00.000Z', '2020-01-01T00:00:00.000Z', '2020-02-06T00:00:00.575Z']}}]

我特別需要所有“名稱”值屬性,例如“死侍裝備包”,但我不確定如何。 如果我嘗試訪問位置 info['data']['featured']['name'] 位置,它會給我一個錯誤。 此外,每天可能會有不同數量的“名稱”值,因此我需要一種方法來返回所有這些值的屬性。

因此,您需要注意兩種類型的對象。

dict :當您加載 JSON 時,它會存儲為字典 object。 Dict 對象允許您通過鍵訪問值,就像您在此處所做的那樣 -> info['data']['featured']

list : print(info) 顯示 'info' 是一個事物列表。 您可以通過方括號 [] 或僅通過調用print(type(info))來判斷。 訂購了一個列表,以便訪問列表中第一個 object 的名稱,您會說info[0]['name']

要連續獲取列表中的所有對象,可以使用 for 循環:

for x in info: print(x['name'])你可以隨意命名'x'。 該循環只是說“對於此列表中的每個 object,執行以下操作”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM