HarmonyOS HSP获取json文件信息?

在HSP中保存一个本地json文件,在本HSP中使用,获取到的是entry中的信息。

例如在xxx的HSP中的Resource目录下rawfile中一个yyy.json文件,获取此文件中的信息

let moduleContext = GlobalContext.getContext().createModuleContext("mgconfigcenter")
moduleContext.resourceManager.getRawFileContent("yyy.json", (error: BusinessError, value: Uint8Array) => {
  if (error != null) {
    console.error("error is " + error);
  } else {
  }
})

使用如下方法可以获取到element/string.json中的字符信息,但是无法获取rawfile中的信息。let result = moduleContext.resourceManager.getStringByNameSync('mgconfigLocal')

请问,如何获取rawfile下的内容。

阅读 570
1 个回答

hsp模块中默认获取的context为entry的context,需要创建moduleContext获取当前hsp模块的context。

一般是先根据模块名创建hsp对应的context,再根据创建的context获取rawfile内的内容。

getContext(this)
  .createModuleContext('hsp的module.json5里面的name')
  .resourceManager
  .getRawFileContent('文件名')
  .then()

可以参考如下调用方式:

getContext(this).createModuleContext('mgconfigcenter').resourceManager.getRawFileContent('yyy.json')
  .then(res=>{
    console.log("error is "+ res.toString())
  }).catch((error:BusinessError) =>{
  console.log(`error is ${JSON.stringify(error)}`);
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进