Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-将字符串拆分为多个json字符串_Python_Python 3.x_Regex_Regex Group - Fatal编程技术网

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

Python-将字符串拆分为多个json字符串,python,python-3.x,regex,regex-group,Python,Python 3.x,Regex,Regex Group,我正在尝试拆分一个字符串并获取其中的所有json字符串 我的字符串: {“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,0,55223]}}}{“数据”:{“类型”:“身份”,“值”:0}{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,0,60,218]}}}{“数据”:{“类型”:“自定义”,“值”:“猫”:“游戏”,“函数”:,“args”:[

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

{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,0,55223]}}}{“数据”:{“类型”:“身份”,“值”:0}{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,0,60,218]}}}{“数据”:{“类型”:“自定义”,“值”:“猫”:“游戏”,“函数”:,“args”:[“动作”,“移动”,“球”,0,65213]}}{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“args”:[“动作”,“移动”,“球”,0,70208]}}

我的正则表达式:

({.*})({.*)
但是,第一组是整个字符串,没有最后一个json字符串

{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,0,55223]}}}{“数据”:{“类型”:“身份”,“值”:0}{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,0,60,218]}}}{“数据”:{“类型”:“自定义”,“值”:“猫”:“游戏”,“函数”:,“args”:[“动作”,“移动”,“球”,0,65,213]}

我想一个接一个的像这样:

{“数据”:{“类型”:“自定义”,“值”:{“猫”:“游戏”,“函数”:“游戏”,“参数”:[“动作”,“移动”,“球”,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]
对于更通用的解决方案,您可以制作一个快速解析器,跟踪平衡括号并生成字符串:

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

这假设大括号已正确平衡。

不要尝试处理JSON字符串。请使用
JSON.loads(…)将其解析为
dict
并使用普通的dict/list操作。@Selcuk这不是一个单独的dict…。或者我应该说,不是一个单独的json字符串。@MarkMeyer哦,对了。格式没有让它变得明显。我想最好的方法是尽可能修复源代码。最后,我没有像你告诉我的那样使用正则表达式。谢谢!
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