鸿蒙Harmony json转对象(1)

案例1

运行代码如下

上图的运行结果如下:

附加1

复制代码
Json_msg interface

案例2

html 复制代码
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

css 复制代码
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[] = JSON.parse(jsonString) as Person[]
        console.debug(`param is ` + persons[0].name);
    }
  }
}
相关推荐
BlackWolfSky1 小时前
鸿蒙高级课程笔记1—应用DFX能力介绍
笔记·华为·harmonyos
盐焗西兰花2 小时前
鸿蒙学习实战之路-Reader Kit阅读服务全攻略
学习·华为·harmonyos
一起养小猫3 小时前
Flutter for OpenHarmony 进阶:数据统计与排序算法深度解析
flutter·harmonyos
果粒蹬i3 小时前
Windows平台ReactNative鸿蒙开发环境搭建指南
windows·react native·harmonyos
jin4213523 小时前
React Native鸿蒙跨平台完成闪屏页作为移动应用的入口级页面,实现的二手置换应用闪屏页SecondhandSplash
javascript·react native·react.js·ecmascript·harmonyos
二蛋和他的大花4 小时前
鸿蒙Next开发实战:如何打造丝滑的运动节拍器——高频音效播放与后台保活避坑指南
华为·harmonyos
摘星编程4 小时前
React Native鸿蒙版:Spinner颜色配置
react native·react.js·harmonyos
一起养小猫4 小时前
Flutter for OpenHarmony 进阶:异步编程与同步机制深度解析
flutter·harmonyos
一起养小猫5 小时前
Flutter for OpenHarmony 进阶:搜索算法与数据持久化深度解析
flutter·harmonyos
一起养小猫6 小时前
Flutter for OpenHarmony 实战:网络监控登录系统完整开发指南
网络·flutter·harmonyos