尝试从python中的Unicode JSON读取内键,值

时间:2018-10-08 19:03:01

标签: python json python-2.7 python-unicode

我正在尝试从Unicode JSON下面获取键值是python

messagejson={
u'Records': 
[
{
u'requestParameters': {u'sourceIPAddress': u'113.112.10.06'}, 
u'sql': {u'configurationId': u'note', 
u'object': {u'eTag': u'ed3645fa5ee', u'sequencer': u'005', u'key': u'src-1.txt', u'size': 606}, 
u'mysql': {u'dn': u'url', u'name': u'invoice',
u'ownerIdentity': {u'principalId': u'A3UJ54'}
}, 
u'myval': u'1.0'
}, 
u'ee': {u'abc': u'tcCh6T', u'x': u'487DBE36E1'}, 
u'userIdentity': {u'principalId': u'AWS:AIDAIOJ4'}, 
u'eventSource': u'source'
}
]
}

使用

尝试

print messagejson['Records'][0]['sql']['key']

获取错误

TypeError: list indices must be integers, not str

有人可以帮忙吗

2 个答案:

答案 0 :(得分:2)

您在字典中缺少一个级别。

运行此命令,您将看到。

print messagejson['Records'][0]['sql']

“键”在“对象”键的值之内,因此您需要先添加“对象”,然后才能获得“键”

print messagejson['Records'][0]['sql']['object']['key']

答案 1 :(得分:1)

您在索引中缺少object级别:

messagejson['Records'][0]['sql']['object']['key']
# src-1.txt