如何写精华回答,获更多曝光?
发布
首先声明喔是小白,但是这个问题已经困扰我很久了,找了很多的资料,看了很多视频,都没能解决,一直卡在这里
同一段字符串,在typescript中运行是正常的,但是在arkts上面运行就是空,求解大神帮忙分析是什么问题
下面是本来的代码,就是简单的处理json字符串
let str='{"code":1,"msg":"\u6570\u636e\u5217\u8868","page":1,"pagecount":3116,"limit":20,"total":62316,"list":[{"vod_id":"61295","vod_name":"\u4f9d\u5170\u7070\u70ec","type_id":"17","type_name":"\u6cf0\u56fd\u5267","vod_en":"yilanhuijin","vod_time":"2024-12-31 16:06:01","vod_remarks":"\u66f4\u65b0\u81f316\u96c6","vod_play_from":"1080zyk"}]}'
let obj=JSON.parse(str) as object
console.log(obj['msg'])
我在DevEco Studio上面的代码是这样子的
export function dataManager(){
let str='{"code":1,"msg":"\u6570\u636e\u5217\u8868","page":1,"pagecount":3116,"limit":20,"total":62316,"list":[{"vod_id":"61295","vod_name":"\u4f9d\u5170\u7070\u70ec","type_id":"17","type_name":"\u6cf0\u56fd\u5267","vod_en":"yilanhuijin","vod_time":"2024-12-31 16:06:01","vod_remarks":"\u66f4\u65b0\u81f316\u96c6","vod_play_from":"1080zyk"}]}'
let obj=JSON.parse(str) as object
console.log(obj['msg'])
return "333"
}
但是运行结果是
01-01 09:37:37.557 26672-21324 A0c0d0/JSAPP W The json.parse interface in the Previewer is a mocked implementation and may behave differently than on a real device.
01-01 09:37:37.557 26672-21324 A0c0d0/JSAPP I undefined
01-01 09:38:17.327 26672-21324 A0c0d0/JSAPP W The json.parse interface in the Previewer is a mocked implementation and may behave differently than on a real device.
01-01 09:38:17.327 26672-21324 A0c0d0/JSAPP I undefined
01-01 09:38:41.391 3428-28256 A0c0d0/JSAPP W The json.parse interface in the Previewer is a mocked implementation and may behave differently than on a real device.
01-01 09:38:41.391 3428-28256 A0c0d0/JSAPP I undefined
我试着去定义接口
import { JSON } from '@kit.ArkTS';
interface vodDataInterface{
vod_id: string;
vod_name: string;
vod_remarks?: string;
vod_play_from?: string;
vod_play_url: string;
vod_actors?: string;
vod_director?: string;
vod_area?: string;
vod_year?: string;
vod_class?: string;
vod_duration?: string;
vod_content?: string;
vod_pic?: string;
vod_score?: string;
vod_play_note?: string;
vod_type?: string;
vod_status?: string;
}
interface httpDataInterface{
code: number;
msg: string;
page: number;
pagecount: number;
limit: number;
total: number;
list: Array<vodDataInterface>
}
export function dataManager(){
let str='{"code":1,"msg":"\u6570\u636e\u5217\u8868","page":1,"pagecount":3116,"limit":20,"total":62316,"list":[{"vod_id":"61295","vod_name":"\u4f9d\u5170\u7070\u70ec","type_id":"17","type_name":"\u6cf0\u56fd\u5267","vod_en":"yilanhuijin","vod_time":"2024-12-31 16:06:01","vod_remarks":"\u66f4\u65b0\u81f316\u96c6","vod_play_from":"1080zyk"}]}'
let obj=JSON.parse(str) as httpDataInterface
console.log(obj.msg)
return "333"
}
运行结果仍然是
01-01 09:54:00.245 17252-19888 A0c0d0/JSAPP W The json.parse interface in the Previewer is a mocked implementation and may behave differently than on a real device.
01-01 09:54:00.245 17252-19888 A0c0d0/JSAPP I undefined
但是在typescript中运行是正常的
到底是为什么呀,话说arkts不是typescript的超集吗?求大神帮忙分析一下