首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python3解析json

使用python3解析json
EN

Stack Overflow用户
提问于 2017-08-02 03:12:05
回答 2查看 857关注 0票数 1

新手python程序员在这里,我有以下json响应:

代码语言:javascript
复制
[
  {
    "type": "Incursion",
    "state": "mobilizing",
    "influence": 1,
    "has_boss": true,
    "faction_id": 500019,
    "constellation_id": 20000739,
    "staging_solar_system_id": 30005054,
    "infested_solar_systems": [
      30005050,
      30005051,
      30005052,
      30005053,
      30005054,
      30005055
    ]
  },
  {
    "type": "Incursion",
    "state": "established",
    "influence": 0,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000035,
    "staging_solar_system_id": 30000248,
    "infested_solar_systems": [
      30000244,
      30000245,
      30000246,
      30000247,
      30000248,
      30000249,
      30000250,
      30000251,
      30000252,
      30000253
    ]
  },
  {
    "type": "Incursion",
    "state": "mobilizing",
    "influence": 0,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000161,
    "staging_solar_system_id": 30001101,
    "infested_solar_systems": [
      30001097,
      30001098,
      30001099,
      30001100,
      30001101,
      30001102
    ]
  },
  {
    "type": "Incursion",
    "state": "established",
    "influence": 0,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000647,
    "staging_solar_system_id": 30004434,
    "infested_solar_systems": [
      30004425,
      30004426,
      30004427,
      30004428,
      30004429,
      30004430,
      30004431,
      30004432,
      30004433,
      30004434,
      30004435
    ]
  },
  {
    "type": "Incursion",
    "state": "established",
    "influence": 0.061500001698732376,
    "has_boss": false,
    "faction_id": 500019,
    "constellation_id": 20000570,
    "staging_solar_system_id": 30003910,
    "infested_solar_systems": [
      30003904,
      30003906,
      30003908,
      30003909,
      30003910,
      30003903
    ]
  }
]

原始代码是用来解析XML响应的。

下面是有问题的代码:

代码语言:javascript
复制
incursion_constellations = []

if (online):
    inc = urllib2.urlopen('https://esi.tech.ccp.is/latest/incursions/')
else:
    inc = file(r'incursions.json', 'r')

jinc = json.load(inc)

for j in jinc['items']:
    incursion_constellations.append(str(j['constellation']['id_str']))


for s in all_stations:
    cur.execute("SELECT constellationID FROM mapSolarSystems WHERE solarSystemID = " + str(s['ssid']))
    res = cur.fetchone()
    cid = str(res[0])
    s['incursion'] = cid in incursion_constellations

我有一个很难理解的地方是:对于jinc‘’items‘中的j

我得到了这个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "./stations.py", line 201, in <module>
    for j in jinc['items']:
TypeError: list indices must be integers, not str

谁能帮助我理解如何将其转换为能够解析json响应、检索constellation_id并将其附加到列表中?

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-02 03:32:07

将原始循环更改为:

代码语言:javascript
复制
for j in jinc:
    incursion_constellations.append(str(j['constellation_id']))

但是您需要确保json中的constellation_id与之前['constellation']['id_str']下的id相同。

票数 2
EN

Stack Overflow用户

发布于 2017-08-02 03:24:59

从响应的开头和结尾看,这个json响应似乎是list,而不是dict,正如您的错误所暗示的那样。

如果它是一个列表,那么您应该使用整数作为索引,而不是像在dict中那样使用str。因此,您的代码应该类似于

代码语言:javascript
复制
jinc[0]['constellation_id']

(我看不出“星座”部分是从哪里来的)

在和中的任何内容都在列表中,并且应该使用整数索引。{和}中的值在dict中,应该使用str索引。

要遍历它,只需使用range和len即可。

类似的问题已经在here上得到了回答。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45445923

复制
相关文章

相似问题

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