需求:读取工程中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)); })
}
运行结果: