HarmonyOS json解析的嵌套对象不走set方法,plainToClass这个方法好像也只对最外层的类有new的效果

示例如下:

import { plainToClass, ClassConstructor } from 'class-transformer'
import "reflect-metadata"
import { BusinessError } from '@kit.BasicServicesKit';
@Observed
class ProductModel{
  name:string = ''
  private _count: number = 0;
  public set count(value: number) {
    this._count = value;
    this.flagCount = value;//最外层这里是走set方法的
  }
  public get count(): number {
    return this._count;
  }
  flag: number = 0;
  details:roductDetailModel[] = []
  flagCount: number = 0;
}
@Observed
class ProductDetailModel{
  detailName:string = ''
  private _detailCount: number = 0;

  public set detailCount(value: number) {
    this._detailCount = value;
    this.flagCount = value;//嵌套的这里就不走set方法了
  }

  public get detailCount(): number {
    return this._detailCount;
  }
  flagCount:number = 0
}
function jsonToArray<T>(cls: ClassConstructor<T>, jsonStr: string): Array<T> {
  try {
    return plainToClass(cls, JSON.parse(jsonStr), { enableImplicitConversion: false, exposeDefaultValues: true }) as Array<T>
  } catch (err) {
    let error = err as BusinessError;
    return []
  }
}
@Entry
@Component
struct ProductListView {
  jsonString:string = '[{"name":"商品1", "count":"1", "details":[{"detailName":"子商品1", "detailCount":"1"}]},{"name":"商品2", "count":"1", "details":[{"detailName":"子商品2", "detailCount":"1"}]},{"name":"商品3", "count":"1", "details":[{"detailName":"子商品3", "detailCount":"1"}] }]'
  @State productList: ProductModel[] = [];
  aboutToAppear(): void {
    this.productList = jsonToArray(ProductModel, this.jsonString);
  }

  build() {
    Column({space:10}){
      ForEach(this.productList,(item:ProductModel)=>{
        ProductView({item:item})
      })
    }.padding(20)
  }
}
@Component
struct ProductView {
  @ObjectLink item: ProductModel;
  build() {
    Column(){
      Row(){
        Text(this.item.name)
        Text(`${this.item.count}`)
        Text(`${this.item.flagCount}`)//最外层这里走了set方法,所以这里的值变了
        Button('数量-1').onClick(()=>{
          this.item.count--
        })
        Button('数量+1').onClick(()=>{
          this.item.count++
        })
      }.width('100%').justifyContent(FlexAlign.SpaceBetween)
      Column({space:10}){
        ForEach(this.item.details,(detailItem:ProductDetailModel)=>{
          ProductDetailsView({parItem:this.item,item:detailItem})
        })
      }.padding(20)

    }
  }
}
@Component
struct ProductDetailsView {
  @ObjectLink parItem:ProductModel;
  @ObjectLink item: ProductDetailModel;
  build() {
    Column(){
      Row(){
        Text(this.item.detailName)
        Text(`${this.item.detailCount}`)
        Text(`${this.item.flagCount}`)//嵌套这里没走set方法,所以这里的值没有发生改变,但是detailCount的值是变了的
        Button('数量-1').onClick(()=>{
          this.parItem.flag --;
          this.item.detailCount--
        })
        Button('数量+1').onClick(()=>{
          this.parItem.flag ++;
          this.item.detailCount ++
        })
      }.width('100%').justifyContent(FlexAlign.SpaceBetween)
    }
  }
}
  • 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.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.

json解析的嵌套对象不走set方法,plainToClass这个方法好像也只对最外层的类有new的效果。

HarmonyOS
2024-12-27 14:15:21
1492浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

请参考以下demo片段,将每一层的Modul数据都plainToClass一下:

aboutToAppear(): void {
  this.productList = jsonToArray(ProductModel, this.jsonString);
  for (let i = 0; i < this.productList.length; i++) {
  let temp: ProductDetailModel[] = this.productList[i].details
  this.productDetailsList = this.productDetailsList.concat(plainToClass(ProductDetailModel, temp) as ProductDetailModel[]) ;
}
console.log("productDetailsList:"+JSON.stringify(this.productDetailsList))
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
分享
微博
QQ
微信
回复
2024-12-27 17:09:13


相关问题
new 出来对象如何获取所属
2499浏览 • 1回复 待解决
HarmonyOS JSON解析实体方法无法调用
862浏览 • 1回复 待解决
上下文工具方法哪些?
1084浏览 • 1回复 待解决
HarmonyOS JSON转换后调用对象方法报错
909浏览 • 1回复 待解决
CentOS 7.3 安装 Docker 简单方法
3236浏览 • 1回复 待解决
鸿蒙WebView 层级 总是处于外层
8571浏览 • 3回复 待解决
嵌套JSON字符串解析问题
2938浏览 • 1回复 待解决