参考demo:import { Context } from '@ohos.abilityAccessCtrl'; import buffer from '@ohos.buffer'; @Entry @Component struct Index { private context: Context = getContext(this); private str: string='' getRawFile(): ESObject { //调用getRawFileContent接口获取json文件内容,并读为string getContext(this).resourceManager.getRawFileContent("a.json", (err, data) => { try { this.str = buffer.from(data.buffer).toString(); } catch (e) { console.info(JSON.stringify(e)) } }) try { let data: Uint8Array = this.context.resourceManager.getRawFileContentSync("a.json"); this.str = buffer.from(data.buffer).toString(); console.log(this.str) } catch (e) { console.info(JSON.stringify(e)) } let obj: ESObject = JSON.parse(this.str) return obj } build() { Column() { Button("get") .onClick(() => { this.getRawFile() }) }.width('100%') } }
参考demo: