Python - 将一个字符串拆分为多个 json 字符串

Python - split a string to multiple json string

我正在尝试拆分字符串并获取其中的所有 json 个字符串
我的字符串:

{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 55, 223]}}}{"datas": {"type": "auth", "value": 0}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 60, 218]}}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 65, 213]}}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 70, 208]}}}

我的正则表达式:

({.*})({.*)

但是,第一组是整个字符串,没有最后一个 json 字符串

{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 55, 223]}}}{"datas": {"type": "auth", "value": 0}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 60, 218]}}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 65, 213]}}}

我想像这样一一得到:

{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 55, 223]}}}

我不知道如何正确解释我的问题,希望你能理解
感谢阅读


**编辑**:最后,我没有使用正则表达式。 这是我的功能:
def packet_to_jsonlist(s):
    jsonlist = []
    count = 0
    current = 0
    for i in range(0, len(s)):
        if s[i] == '{':
            count += 1
        elif s[i] == '}':
            count -= 1
            if count == 0:
                jsonlist.append(s[current:i+1])
                current = i + 1

    return jsonlist

我不认为这是一个很好的通用解决方案,但在这种情况下,您可以在正则表达式上拆分各个字符串,匹配开幕 {。这将为您提供 json 个字符串的列表,然后您可以对其进行解析:

import re
import json

s = '{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 55, 223]}}}{"datas": {"type": "auth", "value": 0}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 60, 218]}}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 65, 213]}}}{"datas": {"type": "custom", "value": {"cat": "game", "func": "game", "args": ["action", "move", "ball", 0, 70, 208]}}}'

js = re.split(r'(?<=})\B(?={)', s)

dicts = [json.loads(s) for s in js]

制作 dicts:

[{'datas': {'type': 'custom',
   'value': {'cat': 'game',
    'func': 'game',
    'args': ['action', 'move', 'ball', 0, 55, 223]}}},
 {'datas': {'type': 'auth', 'value': 0}},
 {'datas': {'type': 'custom',
   'value': {'cat': 'game',
    'func': 'game',
    'args': ['action', 'move', 'ball', 0, 60, 218]}}},
 {'datas': {'type': 'custom',
   'value': {'cat': 'game',
    'func': 'game',
    'args': ['action', 'move', 'ball', 0, 65, 213]}}},
 {'datas': {'type': 'custom',
   'value': {'cat': 'game',
    'func': 'game',
    'args': ['action', 'move', 'ball', 0, 70, 208]}}}]

对于更通用的解决方案,您可以制作一个快速解析器来跟踪平衡括号并生成字符串:

def getGroups(s):
    current = ''
    count = 0
    for c in s:
        if c == '{':
            count += 1
        elif c == '}':
            count -=1 
        current += c
        if count == 0:
            yield current
            current = ''

[json.loads(js) for js in getGroups(s)]
# same output

假设牙套已正确平衡。