JSON.CN让代码更轻
JSON 结构对比可运行案例

传感器读数:解释 readings 首部插入导致的多处差异

数组按下标逐项比较。在 readings 开头插入记录后,后续元素位置移动,会产生多处修改和一个尾部新增;这不等于多条业务记录都发生了真实字段变更。需要按 ID 配对时应先在自己的业务逻辑中处理。

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

示例工作台

在完整工具中打开 ↗
JSON 结构对比修改下方数据后重新运行
运行结果
[
  {
    "op": "replace",
    "path": "/data/readings/0/sensorId",
    "oldValue": "demo-sensors-1",
    "value": "demo-inserted"
  },
  {
    "op": "replace",
    "path": "/data/readings/0/sensorName",
    "oldValue": "演示空气传感器",
    "value": "新增演示记录"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/sensorId",
    "oldValue": "demo-sensors-2",
    "value": "demo-sensors-1"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/sensorName",
    "oldValue": "演示光照传感器",
    "value": "演示空气传感器"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/reading",
    "oldValue": 800,
    "value": 420
  },
  {
    "op": "replace",
    "path": "/data/readings/1/alarm",
    "oldValue": false,
    "value": true
  },
  {
    "op": "replace",
    "path": "/data/readings/1/unit",
    "oldValue": "lux",
    "value": "ppm"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/calibration/version",
    "oldValue": "demo-B",
    "value": "demo-A"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/calibration/points/1",
    "oldValue": 1000,
    "value": 100
  },
  {
    "op": "add",
    "path": "/data/readings/2",
    "value": {
      "sensorId": "demo-sensors-2",
      "sensorName": "演示光照传感器",
      "reading": 800,
      "alarm": false,
      "unit": "lux",
      "calibration": {
        "version": "demo-B",
        "points": [
          0,
          1000
        ]
      }
    }
  }
]

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

⌘ / Ctrl + Enter 运行
01

场景与操作说明

传感器读数需要连同单位和校准条件解释。下面使用两条不同单位的读数,保留传感器标识、告警标记及校准点数组,练习数值提取与数据约束。

reading 是合成非负整数;真实温度等读数可能为负数,不能直接套用本例约束。

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

对照样本

对照样本交换 A 与 B。观察新增与删除的方向、修改前后值如何反转;没有差异时交换仍无差异。

查看对照输入
{
  "ok": true,
  "data": {
    "readings": [
      {
        "sensorId": "demo-inserted",
        "sensorName": "新增演示记录",
        "reading": 420,
        "alarm": true,
        "unit": "ppm",
        "calibration": {
          "version": "demo-A",
          "points": [
            0,
            100
          ]
        }
      },
      {
        "sensorId": "demo-sensors-1",
        "sensorName": "演示空气传感器",
        "reading": 420,
        "alarm": true,
        "unit": "ppm",
        "calibration": {
          "version": "demo-A",
          "points": [
            0,
            100
          ]
        }
      },
      {
        "sensorId": "demo-sensors-2",
        "sensorName": "演示光照传感器",
        "reading": 800,
        "alarm": false,
        "unit": "lux",
        "calibration": {
          "version": "demo-B",
          "points": [
            0,
            1000
          ]
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}
{
  "ok": true,
  "data": {
    "readings": [
      {
        "sensorId": "demo-sensors-1",
        "sensorName": "演示空气传感器",
        "reading": 420,
        "alarm": true,
        "unit": "ppm",
        "calibration": {
          "version": "demo-A",
          "points": [
            0,
            100
          ]
        }
      },
      {
        "sensorId": "demo-sensors-2",
        "sensorName": "演示光照传感器",
        "reading": 800,
        "alarm": false,
        "unit": "lux",
        "calibration": {
          "version": "demo-B",
          "points": [
            0,
            1000
          ]
        }
      }
    ],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}

对照样本的实际结果

[
  {
    "op": "replace",
    "path": "/data/readings/0/sensorId",
    "oldValue": "demo-inserted",
    "value": "demo-sensors-1"
  },
  {
    "op": "replace",
    "path": "/data/readings/0/sensorName",
    "oldValue": "新增演示记录",
    "value": "演示空气传感器"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/sensorId",
    "oldValue": "demo-sensors-1",
    "value": "demo-sensors-2"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/sensorName",
    "oldValue": "演示空气传感器",
    "value": "演示光照传感器"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/reading",
    "oldValue": 420,
    "value": 800
  },
  {
    "op": "replace",
    "path": "/data/readings/1/alarm",
    "oldValue": true,
    "value": false
  },
  {
    "op": "replace",
    "path": "/data/readings/1/unit",
    "oldValue": "ppm",
    "value": "lux"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/calibration/version",
    "oldValue": "demo-A",
    "value": "demo-B"
  },
  {
    "op": "replace",
    "path": "/data/readings/1/calibration/points/1",
    "oldValue": 100,
    "value": 1000
  },
  {
    "op": "remove",
    "path": "/data/readings/2",
    "oldValue": {
      "sensorId": "demo-sensors-2",
      "sensorName": "演示光照传感器",
      "reading": 800,
      "alarm": false,
      "unit": "lux",
      "calibration": {
        "version": "demo-B",
        "points": [
          0,
          1000
        ]
      }
    }
  }
]