json.parse解析出的对象无法判断对象类型

代码如下:

let result: PersonListResponse = JSON.parse(response.result) //解析接口返回数据
let personList: Person[] = result.data.list
personList.foreach((item:Person) => {
  doSomething(item)
})

doSomething<T>(param: T) {
  if(param instanceof Person) {//会是false
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

这种场景下,由json.parse解析出的对象无法通过instanceof条件验证,ArkTS框架下有新的json解析库可用吗?或者有什么办法可以解决上述问题吗?

HarmonyOS
2024-12-28 08:07:55
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以 通过引入三方库 reflect-metadata 和 class-transformer 来实现

reflect-metadata 地址: https://ohpm.openharmony.cn/#/cn/detail/reflect-metadata

class-transformer 地址: https://ohpm.openharmony.cn/#/cn/detail/class-transformer

参考demo:

// test.ts :
export let jsonString:Record<string, ESObject> = {
  "data" : [
    {
      "number" :1,
      "age": 20,
      "testA": {
        "str" : "123"
      }
    },
    {
      "number" :2,
      "age": 21,
      "testA": {
        "str" : "456"
      }
    }
  ]
}

// Index.ets
import { Type, plainToClass } from 'class-transformer'
import 'reflect-metadata'
import { jsonString } from './test'
import { TestA, Person, ViewA} from './ViewA';

class ResponseObj {
  @Type (() => Person)
  data: Person[] = [];
}

@Entry
@Component
struct Index {
  @State list: Person[] = [];
  @State message: string = 'Click me';

  build() {
    Row() {
      Column() {
        Text(this.message).fontSize(40).onClick(() => {
          let responseObj : ResponseObj = plainToClass(ResponseObj, jsonString);
          console.log(` ${responseObj.data[0] instanceof Person}`)
          this.list = this.list.concat(responseObj.data);
        })

        ForEach(this.list, (item : Person, index: number) => {
          ViewA({index:index, testA: item.testA})
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

// ViewA.ets
import { Type } from 'class-transformer'

@Observed
export class TestA {
  public str : string

  constructor(str: string) {
    this.str = str;
  }
}

export class Person {
  name: string = ''
  age: number = 1

  @Type(()=>TestA)
  testA: TestA = new TestA('')
}

@Component
export struct ViewA {
  @ObjectLink testA: TestA
  index: number = -1;

  build() {
    Row(){
      Button(`View A ${this.testA.str}`).onClick(() =>{
        this.index += 1;
        this.testA.str = `${this.index} : Test A String`
      })
    }.margin({top : 10 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
分享
微博
QQ
微信
回复
2024-12-28 10:03:02


相关问题
HarmonyOS JSON.parse 返回类型问题
1144浏览 • 1回复 待解决
HarmonyOS json对象转map类型
782浏览 • 1回复 待解决
JSON.parse数据处理导致精度丢失问题
1091浏览 • 1回复 待解决
json解析对象后获取数据返回undefined。
1420浏览 • 1回复 待解决
HarmonyOS Jsons解析对象无法调用防范
1119浏览 • 1回复 待解决
json如何在ArkTS中解析到Array对象中?
674浏览 • 2回复 待解决
如何获取对象真实类型
910浏览 • 1回复 待解决