首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用FOR循环遍历JSON对象

使用FOR循环遍历JSON对象
EN

Stack Overflow用户
提问于 2021-11-02 00:05:03
回答 1查看 45关注 0票数 0

我目前在一个数据分析新兵训练营,在6周的时间里,我收到了大量的信息。在python和juypter笔记本中工作。我在JSON对象上运行FOR循环时遇到了一些问题。特别希望从json中的每个不同的“full_name”中取出6个“padid”,并按顺序打印它们。我似乎就是不能让我那可怜疲惫的大脑去理解我需要什么语法才能让它运行。任何建议或见解都会非常有帮助!

代码语言:javascript
复制
import requests
import json

url = "https://api.spacexdata.com/v2/launchpads"

space = requests.get(url)

spacex= space.json()
spacex

[{'padid': 5,
  'id': 'vafb_slc_3w',
  'name': 'VAFB SLC 3W',
  'full_name': 'Vandenberg Air Force Base Space Launch Complex 3W',
  'status': 'retired',
  'location': {'name': 'Vandenberg Air Force Base',
   'region': 'California',
   'latitude': 34.6440904,
   'longitude': -120.5931438},
  'vehicles_launched': ['Falcon 1'],
  'attempted_launches': 0,
  'successful_launches': 0,
  'wikipedia': 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_3',
  'details': 'SpaceX original west coast launch pad for Falcon 1. Performed a static fire but was never used for a launch and abandoned due to scheduling conflicts.'},
 {'padid': 2,
  'id': 'ccafs_slc_40',
  'name': 'CCAFS SLC 40',
  'full_name': 'Cape Canaveral Air Force Station Space Launch Complex 40',
  'status': 'active',
  'location': {'name': 'Cape Canaveral',
   'region': 'Florida',
   'latitude': 28.5618571,
   'longitude': -80.577366},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 61,
  'successful_launches': 59,
  'wikipedia': 'https://en.wikipedia.org/wiki/Cape_Canaveral_Air_Force_Station_Space_Launch_Complex_40',
  'details': 'SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15.'},
 {'padid': 8,
  'id': 'stls',
  'name': 'STLS',
  'full_name': 'SpaceX South Texas Launch Site',
  'status': 'under construction',
  'location': {'name': 'Boca Chica Village',
   'region': 'Texas',
   'latitude': 25.9972641,
   'longitude': -97.1560845},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 0,
  'successful_launches': 0,
  'wikipedia': 'https://en.wikipedia.org/wiki/SpaceX_South_Texas_Launch_Site',
  'details': 'SpaceX new launch site currently under construction to help keep up with the Falcon 9 and Heavy manifests. Expected to be completed in late 2018. Initially will be limited to 12 flights per year, and only GTO launches.'},
 {'padid': 1,
  'id': 'kwajalein_atoll',
  'name': 'Kwajalein Atoll',
  'full_name': 'Kwajalein Atoll Omelek Island',
  'status': 'retired',
  'location': {'name': 'Omelek Island',
   'region': 'Marshall Islands',
   'latitude': 9.0477206,
   'longitude': 167.7431292},
  'vehicles_launched': ['Falcon 1'],
  'attempted_launches': 5,
  'successful_launches': 2,
  'wikipedia': 'https://en.wikipedia.org/wiki/Omelek_Island',
  'details': 'SpaceX original launch site, where all of the Falcon 1 launches occured. Abandoned as SpaceX decided against upgrading the pad to support Falcon 9.'},
 {'padid': 6,
  'id': 'vafb_slc_4e',
  'name': 'VAFB SLC 4E',
  'full_name': 'Vandenberg Air Force Base Space Launch Complex 4E',
  'status': 'active',
  'location': {'name': 'Vandenberg Air Force Base',
   'region': 'California',
   'latitude': 34.632093,
   'longitude': -120.610829},
  'vehicles_launched': ['Falcon 9'],
  'attempted_launches': 16,
  'successful_launches': 16,
  'wikipedia': 'https://en.wikipedia.org/wiki/Vandenberg_AFB_Space_Launch_Complex_4',
  'details': 'SpaceX primary west coast launch pad for polar orbits and sun synchronous orbits, primarily used for Iridium. Also intended to be capable of launching Falcon Heavy.'},
 {'padid': 4,
  'id': 'ksc_lc_39a',
  'name': 'KSC LC 39A',
  'full_name': 'Kennedy Space Center Historic Launch Complex 39A',
  'status': 'active',
  'location': {'name': 'Cape Canaveral',
   'region': 'Florida',
   'latitude': 28.6080585,
   'longitude': -80.6039558},
  'vehicles_launched': ['Falcon 9', 'Falcon Heavy'],
  'attempted_launches': 27,
  'successful_launches': 26,
  'wikipedia': 'https://en.wikipedia.org/wiki/Kennedy_Space_Center_Launch_Complex_39#Launch_Complex_39A',
  'details': 'NASA historic launch pad that launched most of the Saturn V and Space Shuttle missions. Initially for Falcon Heavy launches, it is now launching all of SpaceX east coast missions due to the damage from the AMOS-6 anomaly. After SLC-40 repairs are complete, it will be upgraded to support Falcon Heavy, a process which will take about two months. In the future it will launch commercial crew missions and the Interplanetary Transport System.'}]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-02 00:36:41

spacex只是一个字典列表。您可以像遍历其他列表一样遍历它。

代码语言:javascript
复制
for site in spacex:
    print(site['full_name'])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69804035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档