Python json.load JSONDecodeError: Extra data error when trying load multiple json dictionaries

Python json.load JSONDecodeError: Extra data error when trying load multiple json dictionaries

所以我必须以如下所示的相同格式导入 json 文件:

{
    "name": "bob"
}
{
    "name": "sarah"
}

这是我试图用来打开它的函数:

def read_json_file(file):
    with open(file, "r") as r:
        response = json.load(r)
        return response

我在尝试加载时遇到此错误:

json.decoder.JSONDecodeError: Extra data: line 4 column 1 (char 22)

我无法修复 json 数据,因为文件很大。我需要一种解决方法来解析每个字典。

问这个问题的时候我已经试过方法了:

Python json.loads shows ValueError: Extra data

我尝试更改函数以匹配最佳答案:

    response = json.dumps(r)

然而,这导致了这个错误:

TypeError: Object of type TextIOWrapper is not JSON serializable

如有任何帮助,我们将不胜感激。

为了解决那种“多个”/“无效”JSON,你可以读取整个文件,添加这些括号[]来封装字符串,然后将其作为字符串加载json.loads().

  1. 将整个文件作为字符串读取,并将其存储到变量中。
  2. 删除所有出现的换行符和空格。
  3. 添加逗号 , 交集 }{,所以它将是 ...},{....
  4. 用括号[].
  5. 封装
  6. 使用json.loads()解析JSON字符串。

完整代码:

def read_json_file(file):
    with open(file, "r") as r:
        response = r.read()
        response = response.replace('\n', '')
        response = response.replace('}{', '},{')
        response = "[" + response + "]"
        return json.loads(response)

您可以使用 JSONDecoder.raw_decode to incrementally consume the input. Here's an example based on the source of decode():

def json_decode_many(s):
  import json
  import json.decoder
  decoder = json.JSONDecoder()
  _w = json.decoder.WHITESPACE.match

  idx = 0

  while True:
    idx = _w(s, idx).end() # skip leading whitespace
    if idx >= len(s):
      break
    obj, idx = decoder.raw_decode(s, idx=idx)
    yield obj

然后用法看起来像

>>> input_string = """
{
  "name": "bob"
}
{
  "name": "sarah"
}
"""
>>> for x in json_decode_many(input_string):
...   print("Decoded:", x)
...
Decoded: {'name': 'bob'}
Decoded: {'name': 'sarah'}