HarmonyOS JSON转实体类

现在支持json字符串转范型对象的API么

HarmonyOS
2024-12-23 14:35:58
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

请参考:

@Observed class ArticleRes {
  code: number = -1
  data: Array<Article> = [] }
@Observed class Article {
  id: string;
  title: string;
  brief: string;
  isLiked: boolean;
  likesCount: number;
  constructor(id: string, title: string, brief: string, isLiked: boolean, likesCount: number) {
    this.id = id;
    this.title = title;
    this.brief = brief;
    this.isLiked = isLiked;
    this.likesCount = likesCount;
  }
}
res = `{""code"":0,""data"":[
  {""id"":""001"",""title"":""第0篇文章"",""brief"":""文章简介内容"",""isLiked"":false,""likesCount"":100},
  {""id"":""002"",""title"":""第1篇文章"",""brief"":""文章简介内容"",""isLiked"":false,""likesCount"":100},
  {""id"":""003"",""title"":""第2篇文章"",""brief"":""文章简介内容"",""isLiked"":false,""likesCount"":100},
  {""id"":""004"",""title"":""第4篇文章"",""brief"":""文章简介内容"",""isLiked"":false,""likesCount"":100},
  {""id"":""005"",""title"":""第5篇文章"",""brief"":""文章简介内容"",""isLiked"":false,""likesCount"":100},
  {""id"":""006"",""title"":""第6篇文章"",""brief"":""文章简介内容"",""isLiked"":false,""likesCount"":100}
  ]
  }`;

let articleRes: ArticleRes = JSON.parse(this.res)

也可以尝试使用第三方库:class-transformer,进行转换,请参考:

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

分享
微博
QQ
微信
回复
2024-12-23 16:56:30
相关问题
能否支持json实体类的工具
1298浏览 • 1回复 待解决
HarmonyOS 是否有json实体类的工具
289浏览 • 1回复 待解决
HarmonyOS 实体类如何重写toString方法
35浏览 • 1回复 待解决
HarmonyOS JSON解析实体方法无法调用
283浏览 • 1回复 待解决
HarmonyOS JSON怎么Map?
575浏览 • 1回复 待解决
HarmonyOS mapjson对象
393浏览 • 1回复 待解决
HarmonyOS JSON对象失败
399浏览 • 1回复 待解决
HarmonyOS hashmap如何json格式?
449浏览 • 1回复 待解决
HarmonyOS json对象map类型
373浏览 • 1回复 待解决
HarmonyOS mapjson怎么实现?
179浏览 • 1回复 待解决
HarmonyOS 关于JSON模问题
159浏览 • 1回复 待解决
HarmonyOS 关于jsonmodel的处理
182浏览 • 1回复 待解决
HarmonyOS Jsonmap该使用哪个接口
243浏览 • 1回复 待解决
HarmonyOS ArkTS是否支持string的功能
207浏览 • 1回复 待解决