JSON.CN让代码更轻
JSON 整理与类型生成可运行案例

账单明细:把中文字符串写为 Unicode 转义

将 title 等字符串中的非 ASCII 字符写成 Unicode 转义,解析后仍应得到原始数据。它改变书写形式,不是脱敏;拿到结果的人仍可还原中文。

浏览器本地处理合成数据同场景 32 个案例 ↗

示例工作台

在完整工具中打开 ↗
JSON 整理与类型生成prettyUnicode 转义
运行结果
{
  "ok": true,
  "data": {
    "invoices": [
      {
        "invoiceId": "demo-invoices-1",
        "title": "\u6f14\u793a\u670d\u52a1\u8d26\u5355",
        "amountCents": 12000,
        "settled": true,
        "currency": "CNY",
        "lines": {
          "items": [
            {
              "name": "\u6f14\u793a\u670d\u52a1",
              "cents": 12000
            }
          ],
          "taxIncluded": true
        }
      },
      {
        "invoiceId": "demo-invoices-2",
        "title": "\u6f14\u793a\u914d\u989d\u8d26\u5355",
        "amountCents": 3500,
        "settled": false,
        "currency": "USD",
        "lines": {
          "items": [
            {
              "name": "\u6f14\u793a\u914d\u989d",
              "cents": 3500
            }
          ],
          "taxIncluded": false
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}

原例已核验,可修改输入后重新运行。

⌘ / Ctrl + Enter 运行
01

场景与操作说明

账单样本用整数分表示金额,并明确币种。下面的两条账单各有明细数组,适合练习提取账单号、区分金额数字与字符串,以及比较币种或明细变化。

amountCents 是最小货币单位的示例整数;不同币种的最小单位需另行核对。

  1. 核对 账单明细 的字段层级,保留 invoiceId 的字符串类型。
  2. 修改一项输入并运行,观察结果中的值或路径如何变化。
  3. 载入对照样本,复现下面的变化;恢复原例会替换当前输入。
02

对照样本

对照样本只含 ASCII 标识,Unicode 转义不会凭空增加中文或改变字段类型。

查看对照输入
{
  "exampleId": "invoices"
}

对照样本的实际结果

{
  "exampleId": "invoices"
}