我有一个json档案。它的简化版本如下所示:
json
{ "host": "a.com", "ip": "1.2.2.3", "port": 8 } { "host": "b.com", "ip": "2.5.0.4", "port": 3 } { "host": "c.com", "ip": "9.17.6.7", "port": 4 }
我运行此脚本parser.py来解析它:
parser.py
import json from pprint import pprint with open('myfile.json') as f: data = json.load(f) pprint(data)
但我收到此错误:
Traceback (most recent call last): File "parser.py", line 5, in <module> data = json.load(f) File "/usr/lib/python3.6/json/__init__.py", line 299, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) File "/usr/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib/python3.6/json/decoder.py", line 342, in decode raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 6 column 1 (char 54)
您能指出我所缺少的吗?
您的JSON数据集无效,您可以将它们合并为一个对象数组。例如 :
[ { "host": "a.com", "ip": "1.2.2.3", "port": 8 }, { "host": "b.com", "ip": "2.5.0.4", "port": 3 }, { "host": "c.com", "ip": "9.17.6.7", "port": 4 } ]
在JSON中,您不能有多个顶级对象,但可以有对象数组,并且它是有效的
如果需要,可以在此链接中看到更多的JSON数据集示例。