我在这里查看了大部分关于python解析文件中多个json对象的帖子,但是大多数帖子都回答了对象中的对象。
我的json文件
{
"KeyPairs": [
{
"KeyName": "awschrome",
"KeyFingerprint": "f7:cf:a0:54:02:e3:b6:eb:07:05:5d:0a:3e:f9:37:15:06:9e:f1:95"
},
{
"KeyName": "dhepal",
"KeyFingerprint": "ba:61:ed:48:6b:bc:cb:08:2f:0f:ec:3d:c9:65:a4:0d:75:46:b5:6f"
},
{
"KeyName": "load",
"KeyFingerprint": "c3:2d:b9:df:94:5c:93:88:4b:53:1c:e5:49:40:f6:9f:33:cb:9a:12"
},
{
"KeyName": "pal",
"KeyFingerprint": "ac:a6:e9:c5:be:ea:51:2b:25:41:6d:e3:72:ed:5b:87:cd:a2:80:80"
},
{
"KeyName": "terraform_new",
"KeyFingerprint": "94:37:be:54:a2:82:17:84:bc:37:c7:68:2a:41:b6:2f:5d:f3:79:69"
},
{
"KeyName": "vicky2",
"KeyFingerprint": "5b:b9:27:20:c7:fd:c8:09:02:70:d2:5a:95:a3:69:a0:be:fb:17:db"
}
]
}
{
"Tags": [
{
"ResourceType": "snapshot",
"ResourceId": "snap-01a5447df18a68d40",
"Value": "python",
"Key": "Name"
},
{
"ResourceType": "instance",
"ResourceId": "i-017b6d851e312d83a",
"Value": "Terraform_new",
"Key": "Name"
},
{
"ResourceType": "instance",
"ResourceId": "i-063affcbad7bae443",
"Value": "Terraform",
"Key": "Name"
},
{
"ResourceType": "instance",
"ResourceId": "i-096c54c0c00340ef9",
"Value": "Debian",
"Key": "Name"
},
{
"ResourceType": "network-interface",
"ResourceId": "eni-486c4214",
"Value": "dhepal",
"Key": "Name"
},
{
"ResourceType": "volume",
"ResourceId": "vol-04451515102b0bb4c",
"Value": "Terraform_new",
"Key": "Name"
},
{
"ResourceType": "volume",
"ResourceId": "vol-0a0031d456489d18d",
"Value": "Terraform",
"Key": "Name"
},
{
"ResourceType": "volume",
"ResourceId": "vol-0b14e811e9c003744",
"Value": "Debian",
"Key": "Name"
}
]
}
我的代码:
import sys, json, xlwt
with open('json_data_file','r')as f:
data =f.read()
print data
parsed_json=json.loads(data)
print parsed_json
我知道上面的代码不能读取多个json对象并且它提供了错误
File "./xlsconversion.py", line 9, in <module>
parsed_json=json.loads(data)
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 29 column 1 - line 82 column 1 (char 919 - 2335)
Loading and parsing a JSON file with multiple JSON objects in Python
How do I use the 'json' module to read in one JSON object at a time?
上面的链接让我了解了多个json对象,但我没有找到解决我的json文件的方法。 我是python的初学者,请建议我如何使用python读取多个json对象和单个json对象。 注意:我使用的是python2.7