鸿蒙 arkts读取本地json文件

概要

本地json存放位置以及读取json文件

json文件存放位置

在这里插入图片描述

图片存在rawfile中

读取rawfile文件,并将其转为string

import { BusinessError } from '@kit.BasicServicesKit';
import { util } from '@kit.ArkTS';

try {
  getContext().resourceManager.getRawFileContent("文件名.json").then((value : Uint8Array) =>{
  //  let  rawfile = value
    // 将 Uint8Array 转换为字符串
   let  file_data : String = uint8ArrayToString(value)
    console.log("chess_data---",JSON.stringify(file_data))
  }).catch((error : BusinessError) =>{
    console.error("getRawFileContent promise error is " + error);
  })
} catch (e) {
  console.error("error is "+(e as BusinessError).message);
}
// 字节流转成可理解的字符串
function uint8ArrayToString(array: Uint8Array) {
  let textDecoderOptions: util.TextDecoderOptions = {
    fatal: false,
    ignoreBOM: true
  }
  let decodeToStringOptions: util.DecodeToStringOptions = {
    stream: false
  }
  let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions);
  let retStr = textDecoder.decodeToString(array, decodeToStringOptions);
  console.info('字节流转成可理解的字符串:' + retStr);
  return retStr;
}

其他相关方法

import { buffer, util } from '@kit.ArkTS';

// 字符串转成字节流
function stringToUint8Array(str: string) {
  console.info('字符串转成字节流:' + new Uint8Array(buffer.from(str, 'utf-8').buffer));
  return new Uint8Array(buffer.from(str, 'utf-8').buffer);
}

// 字节流转成可理解的字符串
function uint8ArrayToString(array: Uint8Array) {
  let textDecoderOptions: util.TextDecoderOptions = {
    fatal: false,
    ignoreBOM: true
  }
  let decodeToStringOptions: util.DecodeToStringOptions = {
    stream: false
  }
  let textDecoder = util.TextDecoder.create('utf-8', textDecoderOptions);
  let retStr = textDecoder.decodeToString(array, decodeToStringOptions);
  console.info('字节流转成可理解的字符串:' + retStr);
  return retStr;
}

//十六进制转Uint8Array
function HexStrTouint8Array(data: string): Uint8Array {
  console.info('十六进制转Uint8Array:' + new Uint8Array(buffer.from(data, 'hex').buffer));
  return new Uint8Array(buffer.from(data, 'hex').buffer);
}

// Uint8Array转十六进制
function uint8ArrayToHexStr(data: Uint8Array): string {
  let hexString = '';
  let i: number;
  for (i = 0; i < data.length; i++) {
    let char = ('00' + data[i].toString(16)).slice(-2);
    hexString += char;
  }
  console.info('Uint8Array转十六进制:' + hexString);
  return hexString;
}

let uint8Array: Uint8Array;

@Entry
@Component
struct TypeConversion {
  build() {
    Column({ space: 12 }) {
      Button('字符串转成字节流')
        .onClick(() => {
          let str = 'hello';
          uint8Array = stringToUint8Array(str);
        })
      Button('字节流转字符串')
        .onClick(() => {
          if (uint8Array) {
            uint8ArrayToString(uint8Array);
          }
        })
      Button('十六进制转Uint8Array')
        .onClick(() => {
          let data = '05160b22';
          HexStrTouint8Array(data);
        })
      Button('Uint8Array转十六进制')
        .onClick(() => {
          let uint8Array = new Uint8Array([5, 22, 11, 34]);
          uint8ArrayToHexStr(uint8Array);
        })
    }
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43325149

如果我的分享对你造成帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值