From 44dba476c4eedb67ce5f735cf87e1389a222c40d Mon Sep 17 00:00:00 2001 From: sp mac bookpro 2605 Date: Wed, 22 Jul 2026 12:13:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20ExternalToolEditor=20?= =?UTF-8?q?=E7=9A=84=20JSON=20=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E6=9B=B4=E5=AE=BD=E6=9D=BE=E7=9A=84?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=A0=BC=E5=BC=8F=EF=BC=88=E5=A6=82=E6=97=A0?= =?UTF-8?q?=E5=BC=95=E5=8F=B7=E9=94=AE=E5=80=BC=E5=AF=B9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ExternalToolEditor.tsx | 33 +++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/components/ExternalToolEditor.tsx b/src/components/ExternalToolEditor.tsx index 73d771b..851ddd6 100644 --- a/src/components/ExternalToolEditor.tsx +++ b/src/components/ExternalToolEditor.tsx @@ -37,11 +37,11 @@ function parseJsonObject(value: string | undefined, fieldName: string, optional } try { - // 优先尝试标准 JSON 解析 + // 1. 优先尝试标准 JSON 解析 return JSON.parse(trimmedValue); } catch (e) { try { - // 失败后尝试作为 JS 对象解析 (支持无引号、单引号等) + // 2. 失败后尝试作为 JS 对象解析 (支持无引号键、单引号等) // eslint-disable-next-line no-new-func const parsed = new Function(`return (${trimmedValue})`)(); if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { @@ -49,6 +49,35 @@ function parseJsonObject(value: string | undefined, fieldName: string, optional } throw new Error(); } catch (e2) { + // 3. 尝试宽松解析 (处理类似 { Content-Type: application/json } 这种完全无引号的情况) + try { + const content = trimmedValue.replace(/^\{/, '').replace(/\}$/, '').trim(); + const result: Record = {}; + const pairs = content.split(/[\n,]/); + let hasValidPair = false; + + for (let pair of pairs) { + pair = pair.trim(); + if (!pair) continue; + const colonIndex = pair.indexOf(':'); + if (colonIndex > 0) { + const k = pair.substring(0, colonIndex).trim().replace(/^['"]|['"]$/g, ''); + const v = pair.substring(colonIndex + 1).trim().replace(/^['"]|['"]$/g, ''); + if (k) { + let finalVal: any = v; + if (v === 'true') finalVal = true; + else if (v === 'false') finalVal = false; + else if (v === 'null') finalVal = null; + else if (!isNaN(Number(v)) && v !== '') finalVal = Number(v); + result[k] = finalVal; + hasValidPair = true; + } + } + } + if (hasValidPair) return result; + } catch (e3) { + // ignore and fall through to error + } throw new Error(`${fieldName}格式不正确,请确保是有效的 JSON 或对象格式`); } }