TypeError:使用Python解析Json时,字符串索引必须是整数错误 [英] TypeError: string indices must be integers error when parsing Json with Python
问题描述
在您对自己进行判断和思考我已经看过一千遍"之前,请继续阅读并给我机会,我已经在互联网上搜寻答案,但似乎找不到任何帮助.
Before you judge and think to yourself 'I've seen this question a thousand times' please read on and give me a chance, I've scoured the internet for answers but can't seem to find any help.
因此,我正在尝试从使用以下代码获取的API解析json:
So I am trying to parse json from an API which I obtain using the following code:
url = 'https://api.companieshouse.gov.uk/company/' + companyno + '/charges'
r = requests.get(url, auth=('API KEY', ''))
data = r.json()
例如,可以使用以下代码轻松地从json中获取特定值:
I can get specific values from the json pretty easily, for example using this code:
for each in data['items']:
print(each['created_on'])
返回:
2016-11-08
2016-11-08
2016-11-08
2015-03-27
2015-03-27
2015-03-27
2015-03-27
2015-03-27
2007-10-10
2007-09-28
2007-09-19
这正是我想要的,它适用于其他键.
And this is exactly what I want, it works for other keys to.
但是我似乎无法访问json的其中一部分,因此我对json进行了略微编辑,以免发布敏感数据(无论如何,公众都可以使用,但要谨慎),但是基本上没有变化:
However there is one bit of the json which I just can't seem to access, I have slightly edited the json as to avoid releasing sensitive data (which is available to the public anyway but just to be cautious) but it is largely unchanged:
{
'items':[
{
'created_on':'2016-11-08',
'etag':'DELETED',
'classification':{
'type':'charge-description',
'description':'A registered charge'
},
'particulars':{
'contains_negative_pledge':True,
'description':'DELETED',
'type':'brief-description'
},
'transactions':[
{
'links':{
'filing':'DELETED'
},
'filing_type':'create-charge-with-deed',
'delivered_on':'2016-11-21'
}
],
'links':{
'self':'DELETED'
},
'charge_code':'DELETED',
'delivered_on':'2016-11-21',
'status':'outstanding',
'persons_entitled':[
{
'name':'DELETED'
},
{
'name':'DELETED'
}
],
'charge_number':59
},
{
'transactions':[
{
'delivered_on':'2016-11-10',
'links':{
'filing':'DELETED'
},
'filing_type':'create-charge-with-deed'
}
],
'particulars':{
'contains_negative_pledge':True,
'contains_fixed_charge':True,
'floating_charge_covers_all':True,
'contains_floating_charge':True
},
'persons_entitled':[
{
'name':'DELETED'
}
],
'charge_number':58,
'status':'outstanding',
'charge_code':'DELETED',
'links':{
'self':'DELETED'
},
这是[项目,链接,自身]数据
It is the [items, links, self] data
如果我使用以下代码尝试访问它:
If I use the following code to try and access it:
for each in data['items']['links']:
print(each['self'])
我收到以下错误:
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str
如果我尝试使用以下代码访问它:
And if I try to access it using the following code:
for each in data['items'][0]['links']:
print(each['self'])
我收到此错误:
File "<stdin>", line 2, in <module>
TypeError: string indices must be integers
我只是不明白为什么它会向我抛出此错误,互联网上的所有其他响应都指向这样一个事实:我试图解析一个字符串,而不是一个实际的json对象,但是每当我运行命令时:
I just cant understand why its throwing this error at me, all the other responses on the internet point towards the fact that I am trying to parse a string and not an actual json object but whenever I run the command:
type(data)
它返回:
<class 'dict'>
所以我知道这是一本字典,它应该能够通过键进行迭代.
So I know it is a dictionary and that it should be able to iterate through the keys.
我敢肯定我犯了一个非常愚蠢的错误,如果是的话,我道歉,但我无法弄清楚自己在做什么错,有人可以帮忙吗?
I'm sure I'm making a very stupid mistake and if so I apologize but I just can't figure out what I'm doing wrong, can anyone help?
p.s.对不起,很长的帖子.
p.s. sorry for the long post.
非常感谢您的所有答复,现在看来事情变得有意义了.我们都是一个学习者! :)
Thanks so much for all your replies, things seems to make sense now. We were all learners at one point! :)
推荐答案
'links'
键指向字典值,因此对其进行迭代将为您提供字典的键,本例中为
The 'links'
key is pointing to a dictionary value, so iterating over it gives you the key(s) of the dictionary which in this case is 'self'
. You should do:
for k, v in data['items'][0]['links'].items():
if k == 'self':
print(v)
或者您可以直接访问键'self'
上的值,而无需进行迭代:
Or you can simply access the value at key 'self'
without iterating:
print(data['items'][0]['links']['self'])
这篇关于TypeError:使用Python解析Json时,字符串索引必须是整数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!