您可以将生成的数据结构视为字典。在这种情况下,您在结构内部有一个key
被调用,并且该键的 是另一个字典,它有两个键:和。为了访问这些值,您可以使用以下示例中的语法:data
value
internalName
int
In [1]: import json
In [2]: s = '{"data":{"internalName":"value","int":1}}'
In [3]: data = json.loads(s)
In [4]: print data
{u'data': {u'int': 1, u'internalName': u'value'}}
In [5]: data['data']['internalName']
Out[5]: u'value'
所以在你的情况下,你定义后data
,你可以以类似的方式访问它。另外,我建议使用with
打开文件,因为它将为您处理关闭(当您离开缩进块时):
import json
# 'with' is called a context manager, and it handles closing for you when
# you leave the indented block
with open('data.txt', 'r') as f:
data = json.load(f)
# When the code leaves the indented block, the file is closed for you
# Print the entire structure
print json.dumps(data)
# Print out a piece of the data
print data['data']['internalName']