网络返回JOSN数据在本地如何定义适配的数据模型
数据模型类:
export interface IHistoryList{
title:string
lsDate:string
}
export class CHistory {
code: number //成功代码200
msg: string //成功success
data: Array<IHistoryList>
//构造函数
constructor(code: number //成功代码200
, msg: string //成功success
, data: Array<IHistoryList>
) {
this.code = code
this.msg = msg
this.data = data
}
}网络获取数据代码如下:
let httpRequest = http.createHttp()
//console.log('MyTag string=',Constant.historyAPI+selectDate)
httpRequest.request(Constant.historyAPI, {
method: http.RequestMethod.GET,
//header: {},
//expectDataType:http.HttpDataType.OBJECT,
readTimeout: 6000,
connectTimeout: 6000,
}, (err: BusinessError, data: http.HttpResponse) => {
if (!err) {
//获取数据成功
console.info('MyTag 申请数据成功。history data=',JSON.stringify(data.result))
this.history=JSON.parse(data.result.toString()) as CHistory
console.info('MyTag history.code=',this.history.code)
console.info('MyTag history.code=',this.history.msg)
//console.info('MyTag history.code=',this.history.data)
//console.info('MyTag history.code=',this.history.code)
//销毁,释放资源
httpRequest.destroy()
} else {
//获取数据失败
console.info('MyTag 获取数据失败,', JSON.stringify(err))
//销毁,释放资源
httpRequest.destroy()
}
})通过网络获取到数据,结果如下:
11-17 08:53:23.398 3948-11092 A0c0d0/JSAPP I MyTag 申请数据成功。data= "{\"code\":200,\"msg\":\"success\",\"data\":{\"list\":[{\"title\":\"《我是谁\",\"lsdate\":\"1085-01-01\"},{\"title\":\"草原\",\"lsdate\":\"1863-01-01\"}}]}}"
在转换使用时出现如下错误:
11-17 08:53:23.417 3948-11092 C03f00/ArkCompiler E [ArkRuntime Log] TypeError: is not callable
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log]Lifetime: 0.000000s
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log]Js-Engine: ark
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log]page: pages/Index.js
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log]Error message: is not callable
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log]Stacktrace:
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at forEachUpdateFunction (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4339:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:311:25)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at updateFunc (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6776:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at observeComponentCreation2 (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6817:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:271:21)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at ifElseBranchUpdateFunction (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:4286:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at anonymous (entry|entry|1.0.0|src/main/ets/pages/Index.ts:270:17)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at updateFunc (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6776:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at UpdateElement (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6496:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at anonymous (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6719:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at updateDirtyElements (/mnt/disk/workspace/jenkins_cd_pool_high_001/harmony/foundation/arkui/ace_engine/frameworks/bridge/declarative_frontend/engine/stateMgmt.js:6714:1)
11-17 08:53:23.418 3948-11092 C03900/Ace E [Engine Log] at rerender (entry|entry|1.0.0|src/main/ets/pages/Index.ts:328:9)
请问如何定义数据模型类才能与返回的数据匹配?
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题






































苏公安网备 32011402010933号