如何判断两个JSON属性名一致

/**  标准的对比数据*/
let DHCheck = {
        id: "SCD123",
        name: "SCD123",
        style: "2018C287190",
        type: "西裤",
        // url: "mes.etsystem.cn/order/SCD123",
        quantity: 210,
        size_type: 1,
        size_name: {
          base: "M",
          name: ["S", "M", "L"]
        },
        size: [{
          name: "胸围",
          tolerance: 0.0,
          measures: [-1.0, 0.0, 1.0]
        }],
        version: [{
          name: "黑色",
          downloadURL: "Model/123.prj",
          fabric: {
            name: "主面料",
            id: "ZM-173553",
            width: 156.0,
            details: "黑色牛仔",
            len_shrinkage: 0.0,
            width_shrinkage: 0.0,
            price: 30.0,
            amount: 0.0
          },
          quantity: [100, 120, 80]
        }]
      }

      let jsonMes = JSON.parse(this.textarea1)
      let result = this.loopProperty(jsonMes, DHCheck, "根节点")
      if(result!==undefined){
        alert(result)
      }else{
        alert('校验通过')
      }

      //生成不完整测试数据
    },

    /** (对比数据,标准数据,所在的目录) */
    loopProperty(jsonMes, DHCheck, root) {
      for (const item in DHCheck) {
        /**判断有无该属性 */
        if (jsonMes.hasOwnProperty(item)) {
          /**保存当前属性value */
          const element = jsonMes[item]
          /**判断是数组或者对象 */
          if (typeof element == "object") {
            /**判断为数组 */
            if (element.length != undefined) {
              
              /**循环数组 */
              for (let i = 0; i < element.length; i++) {
                /**判断是不是对象数组 */
                if (typeof element[i] != "object") {
                  /**不是对象的话,则不判断 */
                } else {
                  // console.log(element[i])
                  let result = this.loopProperty(element[i], DHCheck[item][0], item+"["+i+"]")
                  if (result != undefined) {
                    return result
                  }
                }
              }
            } else {
              /**如果是对象 */
              let result = this.loopProperty(element, DHCheck[item], item)
              if (result != undefined) {
                return result
              }
            }
          }
        } else {
          return root + "下缺失" + item
        }
      }
    }

 

你可能感兴趣的:(如何判断两个JSON属性名一致)