当前位置: 首页 > article >正文

Harmony OS【获取本地json数据的方法】

// 文件名称 MapContent.json
// 文件路径 resource/rawfile/MapContent.json
[
  {
    "title": "准备与学习",
    "brief": "加入HarmonyOS生态,注册成为开发者,通过HarmonyOS课程了解基本概念和基础知识,轻松开启HarmonyOS的开发旅程。",
    "materials": [
      {
        "subtitle": "HarmonyOS简介",
        "knowledgeBase": [
          {
            "type": "准备",
            "title": "注册账号"
          },
          {
            "type": "准备",
            "title": "实名认证"
          }
        ]
      },
      {
        "subtitle": "赋能套件介绍",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "开发"
          }
        ]
      }
    ]
  },
  {
    "title": "构建应用",
    "brief": "为了帮助开发者更好的理解HarmonyOS提供的能力,我们对重点功能提供了开发指导,辅助开发者完成应用的开发。",
    "materials": [
      {
        "subtitle": "开发工具",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "DevEco Studio"
          }
        ]
      },
      {
        "subtitle": "开发语言",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "ArkTS"
          }
        ]
      },
      {
        "subtitle": "开发框架",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "ArkTS"
          },
          {
            "type": "视频教程",
            "title": "ArkUI之属性动画"
          }
        ]
      },
      {
        "subtitle": "HarmonyOS云开发",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "体验HarmonyOS云开发"
          },
          {
            "type": "指南",
            "title": "云开发"
          },
          {
            "type": "视频教程",
            "title": "HarmonyOS云开发"
          }
        ]
      }
    ]
  },
  {
    "title": "应用测试",
    "brief": "HarmonyOS应用/服务开发完成后,在发布到应用/服务市场前,还需要对应用进行:漏洞、隐私、兼容性、稳定性、性能等测试,确保HarmonyOS应用/服务纯净、安全,给用户带来更好的使用体验。",
    "materials": [
      {
        "subtitle": "",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "云测试"
          },
          {
            "type": "指南",
            "title": "开放式测试"
          }
        ]
      }
    ]
  },
  {
    "title": "上架",
    "brief": "HarmonyOS应用/服务开发、测试完成后,将应用/服务发布至应用市场,用户可以通过应用市场、负一屏等渠道获取到对应的HarmonyOS应用/服务。",
    "materials": [
      {
        "subtitle": "应用发布",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "发布HarmonyOS应用"
          },
          {
            "type": "指南",
            "title": "发布元服务"
          },
          {
            "type": "指南",
            "title": "分阶段发布"
          },
          {
            "type": "视频教程",
            "title": "发布HarmonyOS应用"
          },
          {
            "type": "视频教程",
            "title": "发布元服务"
          }
        ]
      }
    ]
  },
  {
    "title": "运营增长",
    "brief": "HarmonyOS应用/服务发布以后,通过数据及时了解运营情况、质量表现,制定增长策略,借助App Linking、崩溃服务等能力,实现应用及服务的用户增长以及质量提升。",
    "materials": [
      {
        "subtitle": "应用发布",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "应用分析"
          },
          {
            "type": "指南",
            "title": "App Linking"
          },
          {
            "type": "指南",
            "title": "崩溃服务"
          },
          {
            "type": "视频教程",
            "title": "远程配置"
          },
          {
            "type": "视频教程",
            "title": "发布元服务"
          }
        ]
      }
    ]
  },
  {
    "title": "商业变现",
    "brief": "HarmonyOS应用/服务发布以后,通过数据及时了解运营情况、质量表现,制定增长策略,借助App Linking、崩溃服务等能力,实现应用及服务的用户增长以及质量提升。",
    "materials": [
      {
        "subtitle": "",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "流量变现 "
          },
          {
            "type": "指南",
            "title": "联运服务"
          },
          {
            "type": "指南",
            "title": "付费服务"
          },
          {
            "type": "指南",
            "title": "结算指南"
          }
        ]
      }
    ]
  },
  {
    "title": "更多",
    "brief": "",
    "materials": [
      {
        "subtitle": "",
        "knowledgeBase": [
          {
            "type": "指南",
            "title": "常见问题"
          },
          {
            "type": "指南",
            "title": "版本说明"
          }
        ]
      }
    ]
  }
]
// 引入依赖
import { util } from "@kit.ArkTS"
import { BusinessError } from "@kit.BasicServicesKit"

@Component
struct Index {
 @State section:[] = []

  private getSection() {
    try {
      getContext(this).resourceManager.getRawFileContent('MapContent.json', (error: BusinessError, value: Uint8Array) => {
          const textDecoder = util.TextDecoder.create("utf-8");
          const res = textDecoder.decodeToString(value, { stream: false });
          this.section = JSON.parse(res); // 进行转换
      })
    } catch (error) {
      console.error(`报错了:${error}`)
    }
  }

  // 初始话生命周期,在build函数之前执行
  aboutToAppear(): void {
    this.getSection();
  }
 
}

技术点小结

aboutToAppear() {} : 创建自定义组件的新实例后,在执行其build()函数之前执行。 可以运行修改状态变量,更改后将在后续执行build()函数中生效
ps: 自定义组件的生命周期: aboutToAppear


http://www.kler.cn/a/596394.html

相关文章:

  • 字符串A+B
  • 图解 ThreadLocal
  • Linux CenterOS系统ifconfig没有ens33 IP地址解决方法
  • 群体智能优化算法-灰狼优化算法(Grey Wolf Optimizer, GWO,含Matlab源代码)
  • Java-servlet(七)详细讲解Servlet注解
  • 单表达式倒计时工具:datetime的极度优雅(智普清言)
  • 进行光学和傅立叶光学领域的全息图仿真
  • 【leetcode题解】二分算法
  • 【002安卓开发方案调研】之Kotlin+Jetpack开发方案
  • 无人机点对点技术要点分析!
  • mayfly-go开源的一站式 Web 管理平台
  • 【el-upload】el-upload组件 - list-type=“picture“ 时,文件预览展示优化
  • strstr!!!
  • springCloud集成tdengine(原生和mapper方式) 其二 原生篇
  • 【嵌入式学习】计算机自动运行小组件
  • 基于大模型的甲状舌管囊肿全流程预测与临床方案研究报告
  • python学习笔记--实现简单的爬虫(一)
  • vlan路由间配置
  • Pytorch中的torch.utils.data.Dataset 类
  • TSL 和 SSL 是什么?它们有何关系?