HarmonyOS开发实战:使用JSON库数据对象互转

185 篇文章 0 订阅
181 篇文章 1 订阅

 1.鸿蒙系统arkts使用JSON库

JSON库中提供json数据和对象互转方式

interface JSON {
    /**
     * Converts a JavaScript Object Notation (JSON) string into an object.
     * @param text A valid JSON string.
     * @param reviver A function that transforms the results. This function is called for each member of the object.
     * If a member contains nested objects, the nested objects are transformed before the parent object is.
     */
    parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
    /**
     * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
     * @param value A JavaScript value, usually an object or array, to be converted.
     * @param replacer A function that transforms the results.
     * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
     */
    stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
    /**
     * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
     * @param value A JavaScript value, usually an object or array, to be converted.
     * @param replacer An array of strings and numbers that acts as an approved list for selecting the object properties that will be stringified.
     * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
     */
    stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
}

2.示例

2.1.Json文件
{
    "userInfo":{
      "name":"lili",
      "age":30,
      "address":"北京市",
      "account":"ksnowlv"
    },
    "subscriptions":[
      {
        "title":"Flutter入门指南",
        "subtitle":"从零开始学习Flutter",
        "content":"Flutter是一种用于创建跨平台移动应用程序的开源UI工具包。",
        "subscriptionId":"sub001",
        "likes":100
      },
      {
        "title":"Dart编程语言介绍",
        "subtitle":"学习Dart的基本语法和特性",
        "content":"Dart是一种由Google开发的面向对象、类似Java和JavaScript的新型编程语言。",
        "subscriptionId":"sub002",
        "likes":85
      }
    ]
   }

2.2.Json转换为类

export class MyUser {
    userInfo?: UserInfo
    subscriptions?: Subscriptions[]
}

export class Subscriptions {
    title?: string
    subtitle?: string
    content?: string
    subscriptionId?: string
    likes?: number
}

export class UserInfo {
    name?: string
    age?: number
    address?: string
    account?: string
}

2.3.Json转换为对象

    @State myUser: MyUser = null

    try {
        this.myUser = JSON.parse(this.jsonContent);
        const userInfo = this.myUser.userInfo;
        console.info(`${this.TAG} userInfo Name: ${userInfo.name} Age: ${userInfo.age} Address: ${userInfo.address} Account: ${userInfo.account}`)

        const subscriptions = this.myUser.subscriptions;

        subscriptions.forEach((subscription, index) => {
            console.info(`${this.TAG} Subscription ${index + 1}:`);
            console.info(`${this.TAG}  Title: ${subscription.title}`);
            console.info(`${this.TAG}  Subtitle: ${subscription.subtitle}`);
            console.info(`${this.TAG}  Content: ${subscription.content}`);
            console.info(`${this.TAG}  Subscription ID: ${subscription.subscriptionId}`);
            console.info(`${this.TAG}  Likes: ${subscription.likes}`);
        });
    } catch (error) {
        console.error(`${this.TAG} json content:${JSON.stringify(error)}}`)
    }

2.4.对象转换Json

    try {
        let json = JSON.stringify(this.myUser)
        console.info(`${this.TAG} objectToJson jsonString :${json}}`)
    } catch (error) {
        console.error(`${this.TAG} json content:${JSON.stringify(error)}}`)
    }

3.效果

前端页面显示

日志输出

05-29 11:32:26.418  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage userInfo Name: lili Age: 30 Address: 北京市 Account: ksnowlv
05-29 11:32:26.419  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage Subscription 1:
05-29 11:32:26.419  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Title: Flutter入门指南
05-29 11:32:26.419  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Subtitle: 从零开始学习Flutter
05-29 11:32:26.419  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Content: Flutter是一种用于创建跨平台移动应用程序的开源UI工具包。
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Subscription ID: sub001
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Likes: 100
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage Subscription 2:
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Title: Dart编程语言介绍
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Subtitle: 学习Dart的基本语法和特性
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Content: Dart是一种由Google开发的面向对象、类似Java和JavaScript的新型编程语言。
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Subscription ID: sub002
05-29 11:32:26.420  20966-7914   0FEFE/JsApp                                        com.example.base_demo                          I  NetPage  Likes: 85
05-29 11:32:44.805  20966-7914   0FEFE/JsApp                                        com.exa

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

点击领取→【纯血版鸿蒙全套最新学习资料】(安全链接,放心点击希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、(南向驱动、嵌入式等)鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

 鸿蒙(HarmonyOS NEXT)最新学习路线

有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

HarmonyOS Next 最新全套视频教程

 《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

大厂面试必问面试题

鸿蒙南向开发技术

鸿蒙APP开发必备

鸿蒙生态应用开发白皮书V2.0PDF


请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结
总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 

                   

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值