这个工具不错,测试了简单的JSON结构,可用。像上述问题中更复杂的还没测试,当然者也可后台的接口设计有关系,后台接口数据设计的就不能太复杂。
感谢解答!!
ohpm install @yunkss/eftool
=======贴出测试代码=============
import { CacheUtil, JSONArray, JSONObject} from ‘@yunkss/eftool’
import hilog from ‘@ohos.hilog’;
class TestDDD {
threeMonthRate?: string;
moduleName?: string;
productName?: string;
marketPatter?: string;
labelListJson?: site;
}
class site {
site1?: site1;
}
class site1 {
labelType?: string;
labelValue?: string;
labelName?: string;
labelLocation?: string;
}
class User {
id: string;
name: string;
constructor(id: string, name: string) {
this.id = id;
this.name = name;
}
}
class Person {
name: string;
age: number;
isActive: boolean;
birthDate: Date;
primaryUser: User;
userList: User[];
constructor(name: string, age: number, isActive: boolean, birthDate: Date, primaryUser: User, userList: User[]) {
this.name = name;
this.age = age;
this.isActive = isActive;
this.birthDate = birthDate;
this.primaryUser = primaryUser;
this.userList = userList;
}
}
@Entry
@Component
struct Index {
@State message: string = ‘Test Util’;
aboutToAppear(): void {
hilog.info(0xFF00,“AAA”,“aboutToAppear”);
let userList = new Array<User>();
userList.push(new User('2345', '测试用户1'));
userList.push(new User('7844', '测试用户2'));
let person = new Person('测试', 12, true, new Date(), new User("uuid", "打撒吃的是草动次打次"), userList);
let j = JSONObject.toJSONString(person);
hilog.info(0xFF00,"AAA","toJSONString:" + j);
let o = JSONObject.parseObject<Person>(j);
hilog.info(0xFF00,"AAA","Person Age:" + o.userList[0].name);
let str1 = '[{"threeMonthRate":null,"moduleName":"loan","productName":"网E贷","marketPatter":"随借随还,最高20万,利率低至8折起","labelListJson":{"site1":{"labelType":0,"labelValue":"网E贷","labelName":"产品名称:123","labelLocation":true}}},{"threeMonthRate":null,"moduleName":"loan","productName":"烟草贷","marketPatter":"无需抵押,无需担保,当天放款,随借随还","labelListJson":{"site1":{"labelType":12,"labelValue":"烟草贷","labelName":"产品名称","labelLocation":false}}}]';
let array = JSONArray.parseArray<TestDDD>(str1);
array.forEach(item => {
this.message += item.labelListJson?.site1?.labelName + " ";
hilog.info(0xFF00,"AAA","xxx:" + item.productName);
})
}
build() {
RelativeContainer() {
Text(this.message)
.id(‘HelloWorld’)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: ‘container’, align: VerticalAlign.Center },
middle: { anchor: ‘container’, align: HorizontalAlign.Center }
})
}
.height(‘100%’)
.width(‘100%’)
}
}