HarmonyOS axios发送网络请求,默认的序列化model会丢失方法和属性

在使用"@ohos/axios": "2.2.1"发送网络请求时候,指定response的类型是自定义的一个class的model对象,默认的序列化model,除了接口响应返回的属性,在model内部又定义了一些额外属性和方法,但是在使用的时候,额外属性和方法都会丢失无效,该如何处理?

HarmonyOS
2024-12-25 08:54:12
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

axios自带的默认的序列化,会丢失class实例自定义的方法。

1、可以使用自定义序列化实例方法,如上述示例中的fromJson自定义静态方法,从json字符串创建 dataBean实例。

2、可以使用第三方库class-transformer来实现该功能,地址为:

https://gitee.com/openharmony-tpc/openharmony_tpc_samples/tree/master/class-transformer

示例如下:

import { plainToClass } from "class-transformer";

interface UserJson {
  id: number,
  firstName: string,
  lastName: string,
  age: number
}

const userJson: UserJson = {
  id: 1,
  firstName: "Johny",
  lastName: "Cage",
  age: 27
}

class User {
  id: number;
  firstName: string;
  lastName: string;
  age: number;

  constructor() {
    this.id = 0;
    this.firstName = "";
    this.lastName = "";
    this.age = 0;
  }

  getName() {
    return this.firstName + ' ' + this.lastName;
  }

  isAdult() {
    return this.age > 36 && this.age < 60;
  }
}

class DataBean {
  code = -1;
  msg = '';

  isOK() {
    return this.code === 0;
  }

  // 静态方法,从 JSON 字符串创建 DataBean 实例
  static fromJson(jsonString: string): DataBean {
    const jsonData: ESObject = JSON.parse(jsonString);
    const dataBean = new DataBean();
    dataBean.code = jsonData.code;
    dataBean.msg = jsonData.msg;
    return dataBean;
  }
}

@Entry
@Component
struct JSONParseModelPage {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Text(this.message)
        .id('JSONParseModelPageHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })

      Button() {
        Text('自定义序列化')
      }
      .type(ButtonType.Capsule)
      .height(40)
      .width(200)
      .margin({ top: 20 })
      .onClick(() => {
        let json = '{"code":0,"msg":"success"}';
        let dataBean = DataBean.fromJson(json);
        console.log("test", dataBean.msg); // success
        console.log("test", dataBean.isOK());
      })

      Button() {
        Text('transformer1')
      }
      .type(ButtonType.Capsule)
      .height(40)
      .width(200)
      .margin({ top: 20 })
      .onClick(() => {
        const user: User = plainToClass(User, userJson);
        console.log("test", user.getName());
      })

      Button() {
        Text('transformer2')
      }
      .type(ButtonType.Capsule)
      .height(40)
      .width(200)
      .margin({ top: 20 })
      .onClick(() => {
        let userStr = '{"id":2,"firstName":"Tom","lastName": "Jerry","age": 27}'
        let userJson1: UserJson = JSON.parse(userStr)
        const user: User = plainToClass(User, userJson1);
        console.log("test", user.getName());
      })
    }
    .height('100%')
    .width('100%')
  }
}
  • 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.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
分享
微博
QQ
微信
回复
2024-12-25 12:08:42


相关问题
HarmonyOS http请求,json序列化
1069浏览 • 1回复 待解决
HarmonyOS Map 序列化问题
982浏览 • 1回复 待解决
HarmonyOS JSON中map结构如何序列化
1566浏览 • 1回复 待解决
HarmonyOS 有没有类似protobuf序列化
760浏览 • 1回复 待解决
求大佬告知如何序列化对象
1899浏览 • 1回复 待解决
有谁知道 task 参数无法序列化
2380浏览 • 1回复 待解决
自定义类如何实现序列化
2909浏览 • 1回复 待解决
JSON反序列化,如何重命名字段?
1146浏览 • 1回复 待解决
基于@ohos/axios网络请求能力
1380浏览 • 1回复 待解决