【问题标题】:TypeError: string indices must be integers - Parsing JSONTypeError:字符串索引必须是整数 - 解析 JSON
【发布时间】:2016-02-04 21:29:03
【问题描述】:

我在使用以下 JSON 和读取数据时遇到了一些问题,尽管查看了其他一些问题,但似乎没有找到解决方案,除非我遗漏了什么......

帮助总是很感激:)

JSON:

{"ships":{"2":{"name":"Asp","alive":true,"id":2},"3":{"starsystem":{"systemaddress":"670417429889","id":"670417429889","name":"Diaguandri"},"station":{"id":3223343616,"name":"Ray Gateway"},"name":"SideWinder","alive":true,"id":3},"12":{"starsystem":{"systemaddress":"18263140541865","id":"73228","name":"Barnard's Star"},"station":{"id":128147960,"name":"Miller Depot"},"name":"Viper_MkIV","alive":true,"id":12},"13":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"Type7","alive":true,"id":13},"14":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"SideWinder","alive":true,"id":14}}}

Python 代码:

import json

with open('profile.txt') as edstats:
    data = json.load(edstats)

def shipYard():
    ships = [item["name"] for item in data['ships']]
    print json.dumps(ships,indent=4)

错误:

>>> shipYard()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "arg_test_ship.py", line 7, in shipYard
    ships = [item["name"] for item in data['ships']]
TypeError: string indices must be integers

【问题讨论】:

  • 如果回答了您的问题,请接受答案:D

标签: python json


【解决方案1】:

您缺少的问题是 data['ships'] 本身就是另一个字典对象。当你像在 shipYard() 中那样遍历字典时,你只会得到键:

>>> a={'a':1,'b':2}
... [i for i in a]
7: ['a','b']

您想访问字典内的 name 属性,为此您可以使用 dictionary.items() 方法:

>>> data = '''{"ships":{"2":{"name":"Asp","alive":true,"id":2},"3":{"starsystem":{"systemaddress":"670417429889","id":"670417429889","name":"Diaguandri"},"station":{"id":3223343616,"name":"Ray Gateway"},"name":"SideWinder","alive":true,"id":3},"12":{"starsystem":{"systemaddress":"18263140541865","id":"73228","name":"Barnard's Star"},"station":{"id":128147960,"name":"Miller Depot"},"name":"Viper_MkIV","alive":true,"id":12},"13":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"Type7","alive":true,"id":13},"14":{"starsystem":{"systemaddress":"673101653409","id":"673101653409","name":"Brestla"},"station":{"id":3224813312,"name":"Roed Odegaard Port"},"name":"SideWinder","alive":true,"id":14}}}'''
... import json
... data = json.loads(data)
>>> ships = [item['name'] for index, item in data['ships'].items()]
>>> ships
8: [u'Viper_MkIV', u'SideWinder', u'Asp', u'Type7', u'SideWinder']
>>> 

或者,如果您不需要索引,请使用字典 values() 方法:

>>> ships = [item['name'] for item in data['ships'].values()]
>>> ships
9: [u'Viper_MkIV', u'SideWinder', u'Asp', u'Type7', u'SideWinder']
>>> 

【讨论】:

  • 谢谢,这让我发疯了
  • 不用担心。如果您不知道发生了什么,这似乎是基本上不可能注意到的那些功能之一。我遵循的一个很好的经验法则是,在使用列表推导时,如果你没有得到你所期望的,将它拆分成一个 for 循环,打印每次迭代的结果,这样你就可以看到真正发生了什么。
猜你喜欢
  • 2021-08-28
  • 2021-08-01
  • 1970-01-01
  • 2019-04-22
  • 2020-06-24
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多