Uint8Array 如何直接转为String or Json
rawfile文件内存储了一个json类型的文件xx.json,通过如下Api读取,getContext().resourceManager.getRawFileContentSync() 读取的内容为Uint8Array类型,如何转为Json类型的String
HarmonyOS
2024-04-28 00:11:35
浏览
关注
分享
微博
QQ
微信
举报
1
微信扫码分享
删除提问
删除
取消
回答 1
按赞同
/
关注
参考代码:
try {
this.json = String.fromCharCode(...data)
console.info("fromCharCode = " + this.json);
let textDecoderOptions: util.TextDecoderOptions = {
// fatal: false,
ignoreBOM : true
let decodeWithStreamOptions: util.DecodeWithStreamOptions = {
stream: false
let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions);
let retStr = textDecoder.decodeWithStream(data , decodeWithStreamOptions);
console.info("retStr = " + retStr);
let bufferStr = buffer.from(data.buffer);
console.info("bufferStr = " + bufferStr);
}catch (e){
console.info('Cktest ' + JSON.stringify(e))