From beb4fb6a766bcfc9b01d76e0640ec4b117b44cb9 Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Mon, 6 Jul 2026 23:12:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20JSON=20=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=94=AF=E6=8C=81=E5=A4=9A=E8=A1=8C?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96JSON=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/openclaw_extra/client.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/openclaw_extra/client.py b/src/openclaw_extra/client.py index 02f6428..c8c029b 100644 --- a/src/openclaw_extra/client.py +++ b/src/openclaw_extra/client.py @@ -64,13 +64,36 @@ class OpenClawClient: raise Exception(f"CLI error: {result.stderr}") # 解析 JSON 输出(跳过警告) - lines = result.stdout.strip().split("\n") - json_lines = [l for l in lines if l.startswith("{") or l.startswith("[")] + # OpenClaw 输出格式化的多行 JSON,需要提取完整的 JSON 对象 + output = result.stdout.strip() - if json_lines: - return json.loads(json_lines[-1]) + # 找到 JSON 开始位置 + json_start = -1 + for i, char in enumerate(output): + if char == '{' or char == '[': + json_start = i + break - return {} + if json_start == -1: + return {} + + # 提取 JSON 部分 + json_str = output[json_start:] + + try: + return json.loads(json_str) + except json.JSONDecodeError: + # 如果失败,尝试其他方法 + lines = output.split("\n") + json_lines = [l for l in lines if l.strip().startswith("{") or l.strip().startswith("[")] + if json_lines: + # 尝试解析每一行 + for line in json_lines: + try: + return json.loads(line) + except: + continue + return {} except subprocess.TimeoutExpired: raise Exception(f"Timeout running: {cmd}") except json.JSONDecodeError as e: