From 9f64233caeb4f798f5ab0d48e896f4655b75dd4f Mon Sep 17 00:00:00 2001 From: panchengyong Date: Mon, 9 Mar 2026 14:24:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20parseWorkflowRespon?= =?UTF-8?q?se=20=E8=A7=A3=E6=9E=90=20Coze=20=E5=B7=A5=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E5=8F=8C=E7=BC=96=E7=A0=81=20JSON=20=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Coze 工作流返回的 output 字段是双编码的 JSON 字符串: {"output": ""{\\\"calories\\\":...}\""} 修复方案: 1. 第一次解析:JSON.parseObject() 去掉外层引号,得到字符串 2. 第二次解析:JSON.parseObject() 解析该字符串,得到最终的营养数据对象 Co-Authored-By: Claude Opus 4.6 --- .../impl/tool/ToolCommunityServiceImpl.java | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCommunityServiceImpl.java b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCommunityServiceImpl.java index 2e91d2b..ee392fd 100644 --- a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCommunityServiceImpl.java +++ b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolCommunityServiceImpl.java @@ -703,19 +703,49 @@ public class ToolCommunityServiceImpl implements ToolCommunityService { @SuppressWarnings("unchecked") private Map parseWorkflowResponse(Object data) { if (data == null) return null; + + Map result = new HashMap<>(); + Map dataMap = null; + if (data instanceof Map) { - return (Map) data; - } - // 如果是 JSON 字符串,尝试解析 - if (data instanceof String) { + dataMap = (Map) data; + } else if (data instanceof String) { try { - return JSON.parseObject((String) data, Map.class); + dataMap = JSON.parseObject((String) data, Map.class); } catch (Exception e) { log.warn("Failed to parse workflow response as JSON: {}", e.getMessage()); return null; } } - return null; + + if (dataMap == null) return null; + + // 处理 Coze 工作流返回的 output 字段(可能是双编码的 JSON 字符串) + Object outputField = dataMap.get("output"); + if (outputField != null) { + try { + // 第一次解析:从字符串去掉外层引号(因为 output 本身是 JSON 字符串) + String outputStr = outputField instanceof String + ? (String) outputField + : outputField.toString(); + + // 第二次解析:解析内部的 JSON 对象 + Map output = JSON.parseObject(outputStr, Map.class); + result.put("output", output); + } catch (Exception e) { + log.warn("Failed to parse workflow output field: {}", e.getMessage()); + result.put("output", outputField); + } + } + + // 保留其他字段 + dataMap.forEach((key, value) -> { + if (!"output".equals(key)) { + result.put(key, value); + } + }); + + return result; } /**