Python 将JSON数据写入JSON文件时发生JSON.parse错误
我正在尝试从localhost URL逐行获取JSON数据,并将其插入JSON文件中。当我试图打开文件时,我收到一个错误,上面写着Python 将JSON数据写入JSON文件时发生JSON.parse错误,python,json,python-3.x,file-handling,python-jsonschema,Python,Json,Python 3.x,File Handling,Python Jsonschema,我正在尝试从localhost URL逐行获取JSON数据,并将其插入JSON文件中。当我试图打开文件时,我收到一个错误,上面写着多个JSON根元素 import urllib.parse import urllib.request, json import json abc={} for i in range(6666,6669): print(i) full_url="http://localhost/get/info" + str(i) with urllib.
多个JSON根元素
import urllib.parse
import urllib.request, json
import json
abc={}
for i in range(6666,6669):
print(i)
full_url="http://localhost/get/info" + str(i)
with urllib.request.urlopen(full_url) as url:
data = json.loads(url.read().decode())
print(data['id'])
abc={i:[data]}
with open('data.json', 'a') as outfile:
json.dump(abc,outfile)
有效的json对象必须只有一个根 看 必须将所有json对象附加到数组中,然后将其写入json文件 生成的json对象必须与
[
{ object 1 },
{ object 2 }
]
请注意,json中只有一个根对象,即包含所有其他对象的数组实际上,我无法在一个对象中直接获取整个数据。在每一次迭代中,我只得到一行信息,我需要在一个根中写入json文件。我将尝试将其首先放入数组中,然后将其放入json文件中