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

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

相关文章:

  • 【el-upload】el-upload组件 - list-type=“picture“ 时,文件预览展示优化
  • dfs(二十)257. 二叉树的所有路径
  • /proc/[pid]/maps介绍和pmap介绍、RSS
  • 《深入理解 TypeScript:函数类型与泛型全解析》(万字长文)
  • 【MyDB】5-索引管理之 1-索引管理思路概览
  • Centos7配置本地yum源
  • 大白话读懂java对象创建的过程
  • 织梦DedeCMS数据库表说明大全
  • django入门教程之request和reponse【二】
  • Windows 图形显示驱动开发-WDDM 3.0功能- 硬件翻转队列(六)
  • 联想拯救者触摸板会每次开机都自动关闭、联想笔记本触摸板关闭、笔记本电脑触摸板自动关闭的解决方法
  • 演员马晓琳正式加入创星演员出道计划,开启演艺事业新篇章
  • 基于YOLOv8与ByteTrack的车辆行人多目标检测与追踪系统
  • @maptalks/gl-layers中的VectorTileLayer的setStyle属性的全部line配置
  • 群体智能优化算法-模拟退火优化算法(Simulated Annealing, SA,含Matlab源代码)
  • 前端Tailwind CSS面试题及参考答案
  • 实时时钟芯片HYM1381的使用(51单片机)
  • 在K8S中挂载 Secret 到 Pod
  • 【C#知识点详解】ExcelDataReader介绍
  • day3 微机运算基础
  • 广州医药集团有限公司原党委书记、董事长李楚源被“双开”
  • 日本一季度实际GDP环比下降0.2%
  • 埃尔多安:愿在土耳其促成俄乌领导人会晤
  • 车载抬头显示爆发在即?业内:凭借市场和产业链优势,国内供应商实现反超
  • 沙青青评《通勤梦魇》︱“人机组合”的通勤之路
  • 来沪一个月几乎未花住宿钱,女子虚构卫生问题屡薅酒店羊毛被刑拘