// 用ItemModel类接收responseData中JSON字符串的内容
let responseData = `{"id":123,"name":"Example","price":200}` // 在responseData中的price值为number类型的200
class ItemModel {
id?: number;
name?: string;
price?: string; // 作为接收类的ItemModel中price类型为string
}
// 调用JSON.parse()解析JSON字符串,并用as得到ItemModel类型实例
let item = JSON.parse(responseData) as ItemModel;
// 此时得到item.price类型为number而非定义的string
console.info(`typeofprice: ${typeof item.price}`); // 输出为typeofprice:number private options: mapCommon.MapOptions = {
position: {
target: {
latitude: 0,
longitude: 0,
},
zoom: 15,
}
};
let params: Record<string, Object> = {
'latitude': '39.9',
'longitude': '116.4'
}
let lat = params['latitude'] as number;
let lon = params['longitude'] as number;
this.options.position.target.latitude = lat;
this.options.position.target.longitude = lon; 
interface Test {
a: string;
b: string;
}
const a = {
a: 'aa',
b: 'bb',
c: 'cc'
} as Test class ItemModel {
id?: number;
name?: string;
price?: string;
constructor(data: Record<string, Object>) {
this.id = data['id'] as number;
this.name = data['name'] as string;
this.price = String(data['price']); // 将number类型的price转换为string
}
}
let responseData = `{"id":123,"name":"Example","price":200}`;
let parsedData = JSON.parse(responseData) as Record<string, Object>;
let item = new ItemModel(parsedData);
console.info(`typeof price: ${typeof item.price}`); // 输出为typeofprice:string let responseData = `{"id":123,"name":"Example","price":200}`;
// 使用reviver参数在解析时将price的值转换为string类型
let item = JSON.parse(responseData, (key: string, value: Object) => {
if (key === 'price') {
return String(value);
}
return value;
});
console.info(`typeof price: ${typeof item.price}`); // 输出为typeofprice:string 将params类型改为Record<string, number>或者将value值去掉引号(''),在ArkTS中,as关键字是类型断言的一种语法,它不会在运行时改变值的类型,只是在编译阶段告知编译器以特定类型来处理这个值。
断言类型Test与被断言对象的类型不完全一致,阅读示例代码发现类型Test缺少了一个名为c的属性,在类型Test中增加该属性的声明即可。
在ArkTS中,通过HTTP请求获取JSON数据并使用JSON.parse()等方法进行反序列化时,解析过程仅根据JSON字符串中实际存在的字段生成对应数据,不会调用类的构造函数,也不会保留类中定义的默认值。因此,若JSON数据中缺少类中已定义的字段,反序列化后该字段将变为undefined。
目前没有其他自动保留类默认值的方案,开发者需要在反序列化完成后手动检查并为缺失的字段赋默认值,或者在自定义的数据解析逻辑中统一处理字段的默认值。