当前位置:   article > 正文

鸿蒙-简单实现读取资源库(rawfile)自定义json文件_鸿蒙请求本地json文件

鸿蒙请求本地json文件

需求:读取工程中json文件并转成string

步骤一:导入库

import common from '@ohos.app.ability.common';
import util from '@ohos.util';

步骤二:resourcemanager 管理类读取本地host_config.json

aboutToAppear(){

  let context = getContext(this) as common.UIAbilityContext;
  let resourcemanager = context.resourceManager;
  resourcemanager.getRawFileContent("host_config.json").then(value =>{
    let rawFile = value;
    let textdecoder = util.TextDecoder.create('utf-8', { ignoreBOM : true })
    let retStr = textdecoder.decodeWithStream( rawFile , {stream: false});

    console.info('host_config打印: ' + JSON.stringify(retStr));

  })
}

运行结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/979917
推荐阅读
相关标签
  

闽ICP备14008679号