type Item = {x: number, y: string}
type D = {
list: Item[]
}
const json: UTSJSONObject = .... // from uni.request
const result = json.parse<D>(); //will be null
产品分类: uniapp/App
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows 10 Pro 22H2 19045.5011
HBuilderX类型: 正式
HBuilderX版本号: 4.76
手机系统: Android
手机系统版本号: Android 10
手机厂商: 小米
手机机型: 小米10
页面类型: vue
vue版本: vue2
打包方式: 云端
项目创建方式: HBuilderX
type Item = {x: number, y: string}
type D = {
list: Item[]
}
const json: UTSJSONObject = .... // from uni.request
const result = json.parse<D>(); //will be null
const result = json.parse<D>(); //Parse correctly
const result = json.parse<D>(); //will be null
UTSJSONObject.parse方法,如果遇到里面包含对象的数组,就会返回null
2 个回复
DCloud_Android_DQQ
提供一下可以说明问题的最简代码 我看一下
7***@tmp.dcloud.io (作者)
也有可能是嵌套的泛型引起的:
type Item = {x: string};
type Response<D> = {status: string, data: D}
const result = utsJSONObj.parse<Response<Item[]>>(); // result is null
utsJSONObj来自uni.request<UTSJSONObject>....
console.log出来的utsJSONObj的结构和Response<Item[]>是一致的,但是parse返回null
DCloud_Android_DQQ
需要发一下完整的最简示例。只有代码片段的话,我这边无法复现问题。
2025-09-23 10:59
7***@tmp.dcloud.io (作者)
回复 DCloud_Android_DQQ: 随便你们修不修,反正用#ifdef绕过去了
2025-09-29 14:48