json 如何转换成自定义的业务类对象?

如题:json 如何转换成自定义的业务类对象?

阅读 759
1 个回答

ArkTS中已禁用Object.assign,其他禁用的接口可以查看参考文档可以通过反射的Reflect.set(target, propertyName, value)这个方法对基础类型的对象属性进行遍历赋值。基于此实现把json字符串转为自定义类对象,可参考Demo

示例代码

// 属性填充器接口定义转换属性填充器接口
export interface PropertyFiller {
  /**
   * 填充object类型的属性
   *
   * @param key 属性名称
   * @param value 属性值
   */
  fillObjectProperties(key: string, value: Record<string, Object>): void

  /**
   * 填充数组类型的属性
   *
   * @param key 属性名
   * @param value 属性值
   */
  fillArrayProperties(key: string, value: Object[]): void
}

//定义接口实现类,主要实现构造函数中调用设置属性值的方法

// 属性填充器接口抽象实现类,定义入参构造函数并调用初始化属性方法;接口空实现,子类按需实现
export abstract class PropertyFillerImpl implements PropertyFiller {
  /**
   * 构造函数
   *
   * @param value json对象型入参
   */
  constructor(value?: Record<string, Object>) {
    if (value !== undefined && value !== null) {
      // 如果入参有值,则调用工具类填充属性值
      Objects.fillProperties(this, value)
    }
  }

  fillObjectProperties(key: string, value: Record<string, Object>): void {
  }

  fillArrayProperties(key: string, value: Object[]): void {
  }
}

//接上一条定义工具类,主要实现属性值类型的区分赋值

// 工具类
export class Objects {
  /**
   * 把data中的属性赋值给target对象中的数据
   *
   * @param target
   * @param data
   */
  static fillProperties<T extends PropertyFiller>(target: T, data: Record<string, Object>): void {
    if (target === undefined || target === null || data === undefined || data === null) {
      // 如果target或者data为空,则不赋值
      return
    }
    if (typeof target !== 'object') {
      // 如果target不是对象类型,不允许赋值
      return
    }
    Object.keys(data).forEach((key: string): void => {
      let value: Object = data[key]
      if (typeof value === 'string') {
        Reflect.set(target, key, value as string)
      } else if (typeof value === 'boolean') {
        Reflect.set(target, key, value as boolean)
      } else if (typeof value === 'number') {
        Reflect.set(target, key, value as number)
      } else if (typeof value === 'object') {
        if (Array.isArray(value)) {
          // 如果value是数组,则调用填充数组属性的方法
          target.fillArrayProperties(key, value as Object[])
        } else {
          // 如果value是object,则调用填充object属性的方法
          target.fillObjectProperties(key, value as Record<string, Object>)
        }
      } else {
        console.log(TAG, "not support [" + key + "] type: " + typeof value)
      }
    })
  }
}

//定义Bean类

export class TestA extends PropertyFillerImpl {
  name?: string
  size?: number
  isTest?: boolean
  testB?: TestB
  testC?: TestC
  testD?: TestD[]

  fillObjectProperties(key: string, value: Record<string, Object>) {
    if (key === "testB") {
      this.testB = new TestB(value)
    } else if (key === "testC") {
      this.testC = new TestC(value)
    } else {
    }
  }

  fillArrayProperties(key: string, value: Object[]) {
    if (key === "testD") {
      this.testD = value.map((item: Object): TestD => new TestD(item as Record<string, Object>))
    } else {
    }
  }
}


class TestB extends PropertyFillerImpl {
  a?: string
  b?: number
}

class TestC extends PropertyFillerImpl {
  a?: string
  b?: number
  d?: TestD

  getDe(): boolean {
    return this.d.e
  }

  fillObjectProperties(key: string, value: Record<string, Object>) {
    if (key === "d") {
      this.d = new TestD(value)
    }
  }
}

class TestD extends PropertyFillerImpl {
  e?: boolean
  f?: string
}

//业务中使用代码

let jsonString: string = `
{
"isTest": true,
"name": "name value",
"size": 15,
"testB": {
"a": "bbbbbbbbbbbaaaaaa"
},
"testC": {
"a": "caa",
"b": 111,
"d": {
"e": false,
"f": "ffffff"
}
},
"testD": [
{
"e": true,
"f": "dddddddd"
},
{
"e": false,
"f": "dddddddd2"
}
]
}
`
let testA = new TestA(JSON.parse(jsonString))
console.log("testA", JSON.stringify(testA))

参考链接

限制使用标准库

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进