有个对象,例如
const data={
age:12,
list:[
1,2,3,4
]
}
有个函数如下
export function getValueByPath(obj:UTSJSONObject, path:string):any {
const current= obj.getAny(path) as any;
// 返回最终的值
return current;
}
期待
通过执行getValueByPath("xx.xx")来访问内部的值,不管存不存在,不管返回类型
实现
//一定要as any,不能as unkonw,编译成android会报错,
//开发工具:web提示是as unkonw再 as UTSJSONObject
//记得不要这样!!!!!!,一定要as any
const obj=this.obj as any as UTSJSONObject;
this.readData=JSON.stringify(getValueByPath(obj,this.path));
try{
const realData=JSON.parse(this.readData)
//取出来后再判断类型即可
console.log("typeof",typeof realData,realData instanceof Array)
}catch(err){
console.error("err",err)
}