# JSON 结构与字段类型错误 使用 `wikiJsonToXml()`、`submitJsonToXml()`、`parseWikiJson()` 或 `parseSubmitJson()` 解析 JSON 输入时,转换器会逐层验证 JSON 语法、顶层结构以及各个关键路径的字段类型。本页汇总全部 JSON 解析与类型相关的 `XmlWikiConversionError`。 ## 类别总览 | 错误消息模式 | 错误类型 | 触发阶段 | | --- | --- | --- | | `Invalid JSON: ` | `XmlWikiConversionError` | JSON 解析 | | `Wiki JSON must be an InfoItem object or an InfoRoot object containing \`data.item\`.` | `XmlWikiConversionError` | 展示态 JSON 解析 | | `Submit JSON must be an object containing \`item\` with \`brief\` and \`document\`.` | `XmlWikiConversionError` | 提交态 JSON 解析 | | `Expected object at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected list at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected string at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected number at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected string list at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected integer at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected positive integer at ''.` | `XmlWikiConversionError` | JSON 字段校验 | | `Expected string at '..content'.` | `XmlWikiConversionError` | JSON 章节文档引用 | | `Expected block ids to be strings.` | `XmlWikiConversionError` | JSON 块解析 | | `Expected child block ids to be strings.` | `XmlWikiConversionError` | JSON 块解析 | | `Expected list item ids to be strings.` | `XmlWikiConversionError` | JSON 列表解析 | | `Unsupported JSON block kind ''.` | `XmlWikiConversionError` | JSON 块解析 | ## `Invalid JSON: ` - **错误类型**:`XmlWikiConversionError` - **问题原因**:传入的字符串不是合法的 JSON(例如截断的响应体、尾随逗号、单引号或非 JSON 错误页 HTML)。 - **修复方式**:先确认接口响应体完整无损,再将其传入 `wikiJsonToXml()` 或 `submitJsonToXml()`。 ## 顶层包装不匹配(展示态 vs. 提交态) 包含两条消息: - `Wiki JSON must be an InfoItem object or an InfoRoot object containing \`data.item\`.` - `Submit JSON must be an object containing \`item\` with \`brief\` and \`document\`.` - **错误类型**:`XmlWikiConversionError` - **问题原因**:混淆了展示态 JSON 与提交态 JSON 的转换函数(例如把 `item/update` 的 `{ item: { brief, document }, commitMsg }` 传给了 `wikiJsonToXml()`,或把 `item/info` 的 `{ data: { item } }` 传给了 `submitJsonToXml()`)。 - **修复方式**:核对数据源的顶层键名——含 `data.item` 或直接为 `InfoItem` 时使用 `wikiJsonToXml()`(CLI `--from json`);含 `item.brief` 与 `item.document` 时使用 `submitJsonToXml()`(CLI `--from submit-json`)。 ## 通用字段类型断言失败(`Expected at ''.`) 解析器按路径校验必需字段的类型,类型不符时抛出下列七条消息之一: - `Expected object at ''.`:期望普通 JSON 对象,实际为 `null`、数组或原始值。 - `Expected list at ''.`:期望 JSON 数组。 - `Expected string at ''.`:期望字符串。 - `Expected number at ''.`:期望有限数值。 - `Expected string list at ''.`:期望全部元素均为字符串的数组。 - `Expected integer at ''.`:期望可解析为整数的值。 - `Expected positive integer at ''.`:期望大于等于 `1` 的整数(例如表格跨度 `rowSpan` / `colSpan`)。 - `Expected string at '..content'.`:章节或选项卡引用对象缺少字符串类型的 `content` 字段(指向 `documentMap` 中的文档 ID)。 - **错误类型**:`XmlWikiConversionError` - **修复方式**:顺着错误消息中 `` 给出的路径检查 JSON 字段,确认没有被手动改坏或留空为 `null`。 ## 块与列表 ID 数组元素类型错误 包含三条消息: - `Expected block ids to be strings.`(文档顶层 `blockIds` 含非字符串项) - `Expected child block ids to be strings.`(引用块、单元格或列表项的 `childIds` 含非字符串项) - `Expected list item ids to be strings.`(列表的 `itemIds` 含非字符串项) - **错误类型**:`XmlWikiConversionError` - **修复方式**:确保所有 `blockIds`、`childIds` 与 `itemIds` 数组内的元素均为字符串形式的块/项 ID。 ## `Unsupported JSON block kind ''.` - **错误类型**:`XmlWikiConversionError` - **问题原因**:`blockMap` 中某个块的 `kind` 字段超出了当前转换器支持的范围(支持 `text`、`quote`、`list`、`externalVideo`、`horizontalLine`、`image`、`table`)。 - **修复方式**:检查上游条目是否使用了平台新增的块类型;若是官方新增格式,需等待 `@eihrteam/xml` 更新支持后再转换。 ## 另见 - [`InfoRoot` 术语](../../glossary/info-root/):区分展示态根对象与内层 `InfoItem` - [`SubmitRoot` 术语](../../glossary/submit-json/):查看提交态载荷结构 - [环引用与参考 JSON 错误](../cycles-and-references/):排查 JSON 块环引用与 `referenceJson` 校验错误 - [映射总览](../../mapping/overview/):查阅 JSON 字段与 XML 元素的对应关系