HarmonyOS 关于json转model的处理?

目前有一个请求,返回值data的格式是:

{"B":"AF","R":"sg","C_zh":"xxx","C_en":"Afghanistan","N":"93"}

如直接转对象的话,格式如下:

export class CountryInfo {
  B?: string
  N?: string
  R?: string
  C_en?: string
  C_zh?: string
}

但是我在UI层使用的适合使用C\_zh这样的命名肯定不合适,所以需要再转一层。

export class CountryInfoBean {
  isoCode?: string
  areaCode?: string
  region?: string
  enName?: string
  zhName?: string

  constructor(data: CurrentCountryInfoBean) {
    this.isoCode = data.B;
    this.areaCode = data.N;
    this.region = data.R;
    this.enName = data.C_en;
    this.zhName = data.C_zh;
  }
}

而且当data为多层的数组等类型时转换需要多次遍历,比较麻烦。有没有类似Java中gson的处理。

public class CountryInfoBean implements Serializable {
  @SerializedName("B")
  private String isoCode;
  @SerializedName("C_en")
  private String enName;
  @SerializedName("C_zh")
  private String zhName;
  @SerializedName("N")
  private String areaCode;
  @SerializedName("R")
  private String region;
  private String firstWord;
}
阅读 771
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进