鸿蒙征文|【HarmonyOS NEXT】ArkTS 中Json 转Class
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
1. 背景
由于本菜鸡是从Android 开发转而学习HarmonyOS NEXT开发的,所以在ArkTS中解析接口返回的JSON数据时,习惯将JSON字符串转为Data Class 而不是前端中的interface 或者JSONObject。
2. 问题
在ArkTS中,想要将JSON 转为Class,最常见的方法就是 as Class,但是这种写法有一个很严重的问题,就是as 后的Class 中的function 方法丢失了,例如下面的代码,使用class中的isOk 方法时,会报错提示找不到该方法。
class DataBean {
code = -1
msg = ""
isOk() {
return this.code == 0
}
}
let json = `{"code":0,"msg":"success"}`
let dataBean = JSON.parse(json) as DataBean
console.log(dataBean.msg)//success
console.log(`${dataBean.isOk()}`);//error 报错 is not callable
3. 解决办法
使用框架 https://ohpm.openharmony.cn/#/cn/detail/class-transformer
关于 class-transformer 更详细的介绍可以看这里 https://juejin.cn/post/6904890590602330119
3.1. 安装
ohpm i class-transformer
3.2. 封装
//JSONUtils.ts
import { plainToClass, ClassConstructor, instanceToPlain } from "class-transformer";
export default class JSONUtils {
/**
* JSON字符串转Class对象
* @param cls 类名
* @param jsonStr json 字符串
* @returns class对象
*/
static json2Bean<T>(cls: ClassConstructor<T>, jsonStr: string): T | null {
try {
return plainToClass(cls, JSON.parse(jsonStr), {
enableImplicitConversion: false, exposeDefaultValues: true
}) as T
} catch (e) {
return null
}
}
/**
* 对象转字符串
* @param data
* @returns 字符串
*/
static bean2Json(data: Object | Array<Object | String | Number | Boolean> | null | undefined): string {
try {
if (data == null || data == undefined) {
return ''
}
return JSON.stringify(instanceToPlain(data))
} catch (e) {
return ""
}
}
/**
* JSON转Map
* @param jsonStr
* @returns
*/
static json2Map(jsonStr: string): Map<string, Object> {
return new Map(Object.entries(JSON.parse(jsonStr)));
}
}
3.3. 使用
import JSONUtils from '../JSONUtils'
let json = `{"code":0,"msg":"success"}`
let dataBean = JSONUtils.json2Bean(DataBean,json)
console.log(dataBean?.msg)//success
console.log(`${dataBean?.isOk()}`)//true
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 3 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 3 天前
更多推荐
已为社区贡献2条内容
所有评论(0)