DevTools

JSON格式化器

在 Ali Hub DevTools 中使用 JSON格式化器,并获得清晰的验证、结果、隐私说明和已记录限制。

主要输入

仅当工具明确说明浏览器本地、带版本、限定范围、可清除且非敏感的数据时才使用存储。

安装此工具

将此工具添加到设备,以便快速访问。

JSON格式化器

为有效 JSON 添加清晰缩进,查看嵌套数据,并在不改变数据模型的情况下选择缩进宽度。

让 JSON 更容易阅读

紧凑的 JSON 适合传输,但在开发过程中不容易检查。格式化工具会用换行和一致的缩进展示对象与数组,帮助你快速了解层级、比较字段并发现意外结构。

格式化属于展示操作。它的作用是帮助阅读有效文档,不应改变其中的值和关系。如果源文本不正确,请先修复引号、逗号或括号;增加空格无法修复语法错误。

JSON 格式化的含义

JSON 是一种文本格式,由对象、数组、字符串、数字、布尔值和 null 组成。格式化工具读取这种结构,再用换行和空格重新输出。缩进只控制可见的层级,不会添加字段、排序键,也不会执行 JSON Schema 校验。

标准 JSON 使用双引号表示成员名称和字符串。注释、末尾逗号、NaN 和 Infinity 可能出现在某些语言扩展中,但不属于标准语法。因此,熟悉的 JavaScript 对象字面量不一定是有效 JSON。

使用格式化工具

  1. 把 JSON 文档粘贴或输入到源文本框。
  2. 选择数字形式的缩进,例如两个或四个空格。
  3. 运行格式化并查看结果。如果出现错误,请修复源文本后重试。
  4. 复制结果前,比较键、类型和具体值。

格式化方法

  • 先解析

    在安排布局之前,输入必须先被解释为一个 JSON 值。这样可以把语法错误和视觉布局区分开。

  • 缩进嵌套值

    当层级较深时,对象和数组会分布到多行。选定的缩进会在每一层重复使用。

  • 输出易读文本

    结果是已解析值的新文本表示。空白可能改变,但预期的数据结构保持不变。

结果易读并不代表必填字段、业务规则或数据模式都已通过检查。

阅读结果

请同时把结果当作文本和数据检查。确认键、数组顺序、类型和字符串内容保持正确。增加的换行和空格是格式化的正常结果。

错误表示源文本无法按接受的 JSON 语法解析。请检查问题附近的逗号、引号、括号和转义字符。

常见错误

JavaScript 对象字面量、YAML 或带注释的 JSON 可能导致失败。请使用双引号、没有末尾逗号的标准 JSON。

缩进不会排序键,也不会检查必填字段。请把这些工作与格式化分开。

限制

界面展示了源文本和数字缩进。这并不能证明支持 JSON Schema、JSON5、格式转换或文件上传。

大型文档、重复键、数字精度和应用规则,应在实际消费数据的系统中单独测试。

Use the documented workflow, compare the result with the source, and verify live behavior before relying on this output in production work.

Keep sensitive values out of examples and apply the target platform rules, tests, and review process for the selected locale.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Review the localized result against the source, the documented contract, and live behavior before publication or production use.

Examples

格式化紧凑对象

怎样阅读只有一行的对象?

使用两个空格的缩进提交对象。

每个属性出现在单独的行中,名称和值保持不变。

展示嵌套数组

怎样检查对象数组?

使用两个空格运行格式化。

括号和对象边界显示了数组的层级结构。

选择四个空格

什么时候适合更宽的缩进?

选择四个空格并格式化源文本。

每个嵌套层级更加分开,在大屏幕上可能更容易阅读。

发现缺少引号

名称没有引号时会发生什么?

提交源文本并查看响应。

这不是标准 JSON,因为成员名称必须是带双引号的字符串。

保留布尔值

true 会变成字符串吗?

格式化对象并比较值的类型。

true 仍然是布尔值,带引号的 yes 仍然是字符串。

保留 null

null 如何显示?

格式化对象并读取这个值。

null 仍是明确的 JSON 值,与缺少属性或空字符串不同。

Frequently asked questions

格式化会验证 JSON 吗?

源文本必须先解析,因此语法错误可能阻止结果。但它不会验证模式、必填字段或业务规则。

我的值会改变吗?

预期变化是空格和换行。遇到超大数字或重复键时,仍应比较格式化前后的结果。

可以使用单引号吗?

标准 JSON 不可以。名称和字符串必须使用双引号。

为什么末尾逗号会失败?

一些扩展允许末尾逗号,但标准语法不允许。格式化前请删除它。

应该使用两个还是四个空格?

按照项目或评审的习惯选择。它会改变可读性,不会改变数据模型。

工具会排序键吗?

不要默认会排序。格式化只整理布局;排序需要明确记录的选项。

工具能修复错误 JSON 吗?

不能。请在源文本中修复引号、逗号、括号和转义字符。

格式化后的 JSON 更小吗?

通常不会,因为增加了空格。紧凑 JSON 适合传输,格式化 JSON 适合人工检查。

References