HarmonyOS 鸿蒙Next json to class,会导致嵌套的class的@ObjectLink失效
HarmonyOS 鸿蒙Next json to class,会导致嵌套的class的@ObjectLink失效
1、将json字符串使用JSONUtil.jsonToArray转化class[]
2、嵌套的class的 @ObjectLink 失效
3、如果使用JSON.parse会导致 @ObjectLink 整个失效
更多关于HarmonyOS 鸿蒙Next json to class,会导致嵌套的class的@ObjectLink失效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
参考如下代码:
/*import { JSONUtil } from '[@pura](/user/pura)/harmony-utils';*/
import { JSON } from ‘@kit.ArkTS’
class Article {
id: string = ‘’
title: string = ‘’
brief: string = ‘’
isLiked: boolean = false
likesCount: number = 0
items: Item[] = []
constructor(id: string, title: string, brief: string, isLiked: boolean, likesCount: number, items: Item[]) {
this.id = id;
this.title = title;
this.brief = brief;
this.isLiked = isLiked;
this.likesCount = likesCount;
this.items = items;
}
static handleLiked(article: Article) {
console.log(‘123’)
console.log(‘this.article.likesCount’, article.likesCount)
article.isLiked = !article.isLiked;
article.likesCount = article.isLiked ? article.likesCount + 1 : article.likesCount - 1;
}
}
class Item {
id: string = ‘’
name: string = ‘’
constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
}
struct ArticleListView {
@State articleList: Array<Article> = []
aboutToAppear(): void {
let arrt = [
new Article(‘001’, ‘111’, ‘aaaaa’, false, 50, [
new Item(‘a0’,‘aaaaa-0’),
new Item(‘a1’,‘aaaaa-1’),
new Item(‘a2’,‘aaaaa-2’),
]),
new Article(‘002’, ‘222’, ‘bbbbb’, false, 60, [
new Item(‘b0’,‘bbbbb-0’),
new Item(‘b1’,‘bbbbb-1’),
new Item(‘b2’,‘bbbbb-2’)
]),
new Article(‘003’, ‘333’, ‘ccccc’, false, 80, [
new Item(‘c0’,‘ccccc-0’),
new Item(‘c1’,‘ccccc-1’),
new Item(‘c2’,‘ccccc-2’),
])
]
this.articleList = arrt;
/*let str =
‘[{“id”:“001”,“title”:“111”,“brief”:“aaaaa”,“isLiked”:false,“likesCount”:10,“items”:[{“id”:“a0”,“name”:“aaaaa-0”},{“id”:“a1”,“name”:“aaaaa-1”},{“id”:“a2”,“name”:“aaaaa-2”}]},{“id”:“002”,“title”:“222”,“brief”:“bbbbb”,“isLiked”:false,“likesCount”:20,“items”:[{“id”:“b0”,“name”:“bbbbb-0”},{“id”:“b1”,“name”:“bbbbb-1”},{“id”:“b2”,“name”:“bbbbb-2”}]},{“id”:“003”,“title”:“333”,“brief”:“ccccc”,“isLiked”:false,“likesCount”:30,“items”:[{“id”:“c0”,“name”:“ccccc-0”},{“id”:“c1”,“name”:“ccccc-1”},{“id”:“c2”,“name”:“ccccc-2”}]}]’;
this.articleList = JSON.parse(str) as Array<Article>*/
console.log(‘this.articleList’,${<span class="hljs-built_in">JSON</span>.stringify(<span class="hljs-keyword">this</span>.articleList)}
)
/*let str =
‘[{“id”:“001”,“title”:“111”,“brief”:“aaaaa”,“isLiked”:false,“likesCount”:100,“items”:[{“id”:“a0”,“name”:“aaaaa-0”},{“id”:“a1”,“name”:“aaaaa-1”},{“id”:“a2”,“name”:“aaaaa-2”}]},{“id”:“002”,“title”:“222”,“brief”:“bbbbb”,“isLiked”:false,“likesCount”:100,“items”:[{“id”:“b0”,“name”:“bbbbb-0”},{“id”:“b1”,“name”:“bbbbb-1”},{“id”:“b2”,“name”:“bbbbb-2”}]},{“id”:“003”,“title”:“333”,“brief”:“ccccc”,“isLiked”:false,“likesCount”:100,“items”:[{“id”:“c0”,“name”:“ccccc-0”},{“id”:“c1”,“name”:“ccccc-1”},{“id”:“c2”,“name”:“ccccc-2”}]}]’;
this.articleList = JSONUtil.jsonToArray(Article, str)*/
// console.log(${this.articleList.toString()}
)
}
build() {
List() {
ForEach(this.articleList, (item: Article) => {
ListItem() {
ArticleCard({
article: item
})
.margin({ top: 20 })
}
}, (item: Article) => JSON.stringify(item))
}
.padding(20)
.scrollBar(BarState.Off)
.backgroundColor(0xF1F3F5)
}
}
struct ArticleCard {
@ObjectLink article: Article;
/*handleLiked() {
console.log(‘123’)
console.log(‘this.article.likesCount’, this.article.likesCount)
this.article.isLiked = !this.article.isLiked;
this.article.likesCount = this.article.isLiked ? this.article.likesCount + 1 : this.article.likesCount - 1;
}*/
build() {
Column() {
Row() {
Image($r(‘app.media.app_icon’))
.width(80)
.height(80)
.margin({ right: 20 })
Column() {
Text(this.article.title)
.fontSize(20)
.margin({ bottom: 8 })
Text(this.article.brief)
.fontSize(16)
.fontColor(Color.Gray)
.margin({ bottom: 8 })
Row() {
Image(this.article.isLiked ? $r(‘app.media.startIcon’) : $r(‘app.media.app_icon’))
.width(24)
.height(24)
.margin({ right: 8 })
Text(‘likesCount’)
Text(this.article.likesCount.toString())
.fontSize(16)
}
.onClick(() => {
// 通过赋值添加 Proxy 代理
let article1 = this.article;
Article.handleLiked(article1);
})
.justifyContent(FlexAlign.Center)
}
.alignItems(HorizontalAlign.Start)
.width(‘80%’)
.height(‘100%’)
}
// .alignItems(HorizontalAlign.Start)
.width(‘100%’)
Row({space: 10}) {
ForEach(this.article.items, (item: Item) => {
ArticleItem({item: item})
}, (item: Item) => item.id)
}
}
.padding(20)
.borderRadius(12)
.backgroundColor(’#FFECECEC’)
.height(120)
.width(‘100%’)
.justifyContent(FlexAlign.SpaceBetween)
}
}
struct ArticleItem {
@ObjectLink item: Item;
build() {
Text(this.item.name)
.onClick(() => {
console.log(‘111222’)
this.item.name = ${<span class="hljs-keyword">this</span>.item.name} - <span class="hljs-number">0</span>
})
}
}
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>
更多关于HarmonyOS 鸿蒙Next json to class,会导致嵌套的class的@ObjectLink失效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next环境中,处理JSON到类的映射时,若遇到嵌套的class中@ObjectLink
失效的问题,通常是因为JSON解析框架或序列化机制在处理嵌套对象时未能正确识别或应用注解。
在鸿蒙的JSON处理中,确保嵌套类正确标注@ObjectLink
是关键。首先检查嵌套类是否已正确声明并应用@ObjectLink
注解。此外,确认主类中的嵌套类属性也使用了相应的注解来引导JSON解析器正确解析嵌套结构。
若@ObjectLink
在嵌套类中失效,可能是因为:
- 嵌套类的定义或注解使用有误。
- JSON数据结构与类定义不匹配。
- 使用的JSON解析库版本或实现不支持嵌套注解或存在bug。
解决方法包括:
- 重新检查嵌套类的定义和注解使用,确保无误。
- 确认JSON数据格式与类定义严格对应。
- 查阅当前使用的JSON解析库的文档,确认是否支持嵌套注解,或是否存在已知的bug。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html