当前位置: 首页> 教育> 培训 > 网络客服工作平台有哪些_中企动力z邮箱登录入口_高清网站推广免费下载_南京seo网络推广

网络客服工作平台有哪些_中企动力z邮箱登录入口_高清网站推广免费下载_南京seo网络推广

时间:2025/8/6 7:29:28来源:https://blog.csdn.net/u010949451/article/details/144687700 浏览次数:0次
网络客服工作平台有哪些_中企动力z邮箱登录入口_高清网站推广免费下载_南京seo网络推广

【HarmonyOS】如何读取Raw下的JSON文件内容

一、问题背景:

读取工程中/resources下json文件,json文件推荐放在rawfile路径下,通过getRawFileContent接口访问。但是通过该接口拿到的是Uint8Array类型数据,那怎么拿到JSON的文本内容?
在这里插入图片描述

二、解决方案:

1.首先通过getRawFileContent拿到文件的字节流内容。

  getContext().resourceManager.getRawFileContent("vibration.json")

2.通过util.TextDecoder设置编码,解析字节流内容转化为string文本。

import { util, buffer } from '@kit.ArkTS';let decoder = util.TextDecoder.create('utf-8');let json = decoder.decodeToString(rawFile);

三、源码示例:

在这里插入图片描述
JSONTestPage.ets

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

struct JSONTestPage {private TAG: string = "JSONTestPage"; showText: string = "";aboutToAppear(): void {this.getRawJson();}/*** 获取JSON*/private getRawJson(){try {getContext().resourceManager.getRawFileContent("vibration.json").then((value: Uint8Array) => {let rawFile = value;let decoder = util.TextDecoder.create('utf-8');let json = decoder.decodeToString(rawFile);console.info(this.TAG, "json: " + json);this.showText = json;}).catch((error: BusinessError) => {console.error("getRawFileContent promise error is " + error);});} catch (error) {let code = (error as BusinessError).code;let message = (error as BusinessError).message;console.error(`promise getRawFileContent failed, error code: ${code}, message: ${message}.`);}}build() {Scroll(){Row() {Text(this.showText).fontSize(50).fontWeight(FontWeight.Bold)}}.height('100%').width('100%')}
}

在这里插入图片描述
vibration.json

{"MetaData": {"Create": "2023-01-09","Description": "a haptic case","Version": 1.0,"ChannelNumber": 1},"Channels": [{"Parameters": {"Index": 0},"Pattern": [{"Event": {"Type": "transient","StartTime": 0,"Parameters": {"Frequency": 31,"Intensity": 100}}},{"Event": {"Type": "continuous","StartTime": 40,"Duration": 54,"Parameters": {"Frequency": 30,"Intensity": 38,"Curve": [{"Time": 0,"Frequency": 0,"Intensity": 0},{"Time": 1,"Frequency": 15,"Intensity": 0.5},{"Time": 40,"Frequency": -8,"Intensity": 1.0},{"Time": 54,"Frequency": 0,"Intensity": 0}]}}}]}]
}
关键字:网络客服工作平台有哪些_中企动力z邮箱登录入口_高清网站推广免费下载_南京seo网络推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: