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

鸿蒙Harmony json转对象(1)

案例1

 运行代码如下

 上图的运行结果如下:

附加1 

Json_msg interface

案例2

import {JSON } from '@kit.ArkTS';
export interface commonRes {
  status: number
  returnJSON: ESObject;
  time: string
}
export interface returnRes {
  uid: string
  userType: number;
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';

  build() {
    RelativeContainer() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(async (event: ClickEvent) => {
          // 将该json 解析成对象
          let str = '{"returnJSON":{"uid":"02f17bc6b3fc465fab91d083844c7a29","userType":1,"createOn":"2024-07-12 16:38:18","thirdPartys":[],"info":{"cardNo":"AAA02342","radishQty":0},"showPage":false,"userMark":"old","isBlackNight":true,"installChannel":"Harmonyos","adChannel":"Harmonyos","showImg":"https://word.xxx.cn/tools/diary/UserDress/53c36b159ce84cf583db8f7d866cab40.png","IsDollOpen":true,"IsActivityOpen":true,"nickName":"匿名","level":1,"levelNick":"萌新","proType":0,"color":"#D2B7FF","PhotoFrameUrl":""},"status":1,"time":"2024-07-15 03:04:16"}'
          let str1 = '{"returnJSON":{"uid":"02f17bc6b3fc465fab91d083844c7a29","userType":1},"status":1,"time":"2024-07-15 03:04:16"}'

          let obj:commonRes = JSON.parse(str) as commonRes
          console.log("----1-time--"+obj.time)

          //方案一 自定义returnRes类
          let res:returnRes = obj.returnJSON
          console.log("----1-uid-"+res.uid)

          //方案二 不定义returnRes类
          let res1:ESObject = JSON.parse(JSON.stringify(obj.returnJSON))
          console.log("----2-uid-"+res1["uid"])

        })
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
    }
    .height('100%')
    .width('100%')
  }
}

案例3

import { DataAbilityHelper } from '@ohos.data.ability';
 
interface Person {
  name: string;
  age: number;
  city: string;
}
 
@Component
struct JsonToEntity {
  async convertJsonToEntities(jsonString: string) : Person[] {
    let persons: Person[] = JSON.parse(jsonString) as Person[];
    return persons;
  }
 
  build() {
    Column() {
      let jsonString = `[{"name":"徐庆","age":31,"city":"湖北"},{"name":"宇智波斑","age":80,"city":"宇智波"},{"name":"万祐宁","age":30,"city":"湖北"},{"name":"千手柱间","age":79,"city":"木叶"},{"name":"我爱罗","age":28,"city":"沙影村"},{"name":"佩恩","age":34,"city":"雨影村"}]`;
      let persons: Person[] = convertJsonToEntities(jsonString)
        console.debug(`param is ` + persons[0].name);
    }
  }
}


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

相关文章:

  • (undone) 并行计算学习 (Day2: 什么是 “伪共享” ?)
  • 【vitePress】基于github快速添加评论功能(giscus)
  • vulnhub靶场【IA系列】之Tornado
  • AUTOSAR从入门到精通-线控底盘技术
  • Linux磁盘空间不足,12个详细的排查方法
  • STL--list(双向链表)
  • Golang 生态学习
  • Git原理与应用(三)【远程操作 | 理解分布式 | 推送拉取远程仓库 | 标签管理】
  • 网络协议如何确保数据的安全传输?
  • 虚幻商城 Fab 免费资产自动化入库
  • bcache的基本操作
  • PHP装修行业小程序
  • [Collection与数据结构] PriorityQueue与堆
  • 数据结构-LinkedList和链表
  • 《贪心算法:原理剖析与典型例题精解》
  • 2024年AI大模型技术年度总结与应用实战:创新与突破并进
  • Spring Boot Starter探秘:全面了解 spring-boot-starter-web
  • 小程序获取微信运动步数
  • 在 Babylon.js 中使用 BillboardMode:让对象始终面向摄像机
  • 离散数学---期末复习知识点
  • LeetCode 1133:最大唯一数
  • 《A++ 敏捷开发》- 14 精益与敏捷
  • Hadoop 与 Spark:大数据处理的比较
  • MATLAB中while循环例子,for循环嵌套例子
  • 基于深度学习的Lidar 3D点云表面缺陷检测方法
  • 电脑未能成功安装设备驱动程序怎么办 几种有效修复方法