我正在尝试拆分一个字符串并获取其中的所有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字符串列表,然后您可以对其进行解析:制作
dicts
:对于更通用的解决方案,您可以制作一个快速解析器,跟踪平衡括号并生成字符串:
这是假设支架已正确平衡
相关问题 更多 >
编程相关推荐