diff --git a/src/page/home/index.tsx b/src/page/home/index.tsx index 1595481..5e5fb5b 100644 --- a/src/page/home/index.tsx +++ b/src/page/home/index.tsx @@ -26,17 +26,25 @@ export default function Home() { // 计算匹配结果 const result = useMemo(() => { + let parsed + try { + parsed = JSON.parse(jsonInput) + } catch (e) { + return { parsed: null, matchedPaths: [], matchedValues: [], error: (e as Error).message, queryError: null } + } + try { - const parsed = JSON.parse(jsonInput) const nodes = jp.nodes(parsed, query) return { parsed, matchedPaths: nodes.map((n) => jp.stringify(n.path)), matchedValues: nodes.map((n) => n.value), error: null, + queryError: null, } } catch (e) { - return { parsed: null, matchedPaths: [], matchedValues: [], error: (e as Error).message } + // JSONPath 表达式无效时,仍显示 JSON 树,但无匹配 + return { parsed, matchedPaths: [], matchedValues: [], error: null, queryError: (e as Error).message } } }, [jsonInput, query]) @@ -81,12 +89,19 @@ export default function Home() {