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

2024-05-19 18:49:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试拆分一个字符串并获取其中的所有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

Tags: 字符串gamemovevaluetypecountcustomargs
1条回答
网友
1楼 · 发布于 2024-05-19 18:49:33

我不认为这是一个很好的通用解决方案,但是在本例中,您可以在一个正则表达式上拆分单个字符串,该正则表达式与开头{旁边的结束}匹配。这将为您提供一个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

这是假设支架已正确平衡

相关问题 更多 >