【问题标题】:How to read two concatenated JSON files?如何读取两个连接的 JSON 文件?
【发布时间】:2025-12-14 21:05:03
【问题描述】:

我有以下由两个连接的 JSON 字符串组成的文件:

{
  "hello": 2,
  "world": 3
}{
  "something": 5,
  "else": 6
}

它们各自都是正确的(它们比这更复杂,但总是两个一个接一个的 JSON dicts)。

由于我可以预测第一个的格式(接近我上面的示例),我将使用正则表达式解析文件并最终将它们分开(我只需要第二个 JSON):

{[\s\S]*?}([.\n]*?)

虽然此解决方案有效,但我想确保没有更通用的方法来解决此问题。

【问题讨论】:

  • 如果它总是 }{,那么只需 my_string.replace('}{', '},{') 然后 json.loads(my_string),因为它现在应该是有效的。
  • @Artagel:这是一个绝妙的主意,谢谢。想把它变成答案吗?
  • 好的,我加了。

标签: python json python-3.x


【解决方案1】:

raw_decode 将解析一个字符串并返回其对象加上对象序列化结束的索引。只要文档合理地适合内存,您就可以轻咬字符串。

>>> text="""{
...   "hello": 2,
...   "world": 3
... }{
...   "something": 5,
...   "else": 6
... }
... 
... """

>>> import json
>>> decoder = json.JSONDecoder()
>>> text = text.lstrip() # decode hates leading whitespace
>>> while text:
...     obj, index = decoder.raw_decode(text)
...     text = text[index:].lstrip()
...     print(obj)
... 
{'world': 3, 'hello': 2}
{'else': 6, 'something': 5}

【讨论】:

    【解决方案2】:

    您可以通过将字符串转换为有效的 python 对象(如字典列表)来简单地格式化字符串,然后使用 json 模块加载它:

    In [60]: s = """{
      "hello": 2,
      "world": 3
    }{
      "something": 5,
      "else": 6
    }"""
    
    In [61]: json.loads("[{}]".format(s.replace('}{', '},{')))
    Out[61]: [{'hello': 2, 'world': 3}, {'something': 5, 'else': 6}]
    

    【讨论】:

      【解决方案3】:

      试试这个:

      my_str = """{
        "hello": 2,
        "world": 3
      }{
        "something": 5,
        "else": 6
      }"""
      
      fixed_str = my_str.replace('}{', '},{')
      
      my_json = json.loads("[" + fixed_str + "]")
      

      【讨论】: