繁体   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