我有一个以漂亮打印格式存储的大型 JSON 文档,文件如下所示:
$ nano data.json
{
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
我发现读取此类 json 文件的传统方法,例如...
with open('data.json', 'r') as handle:
data = json.load(handle)
和...
json_data=open('data.json','r')
data = json.load(json_data)
json_data.close()
和...
data = []
with open('data.json') as f:
for line in f:
data.append(json.loads(line))
和...
ss = ''
with open('data.json', 'r') as f:
for line in f:
ss += ''.join(line.strip())
data = json.loads(ss.decode("utf-8","replace"))
...似乎只适用于单字符串,而不是漂亮打印格式的 JSON。
如何从文件中加载这种格式的 JSON?我在尝试这些格式时不断遇到的错误是......
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting , delimiter: line 1 column 250 (char 250)
ValueError: Expecting , delimiter: line 9 column 13 (char 310)