鸿蒙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);
    }
  }
}
相关推荐
AI_零食6 小时前
鸿蒙PC Electron跨平台应用开发:24时区时间表应用详解
前端·华为·electron·开源·harmonyos·鸿蒙
提子拌饭13310 小时前
爆发效果技术——基于鸿蒙PC Electron框架实现
华为·架构·electron·开源·harmonyos·鸿蒙·鸿蒙系统
坚果派·白晓明10 小时前
鸿蒙PC三方库使用:使用 AtomCode + Skills 自动完成鸿蒙化三方库spdlog集成
c++·华为·ai编程·harmonyos·skills·atomcode·c/c++三方库
再见65810 小时前
【鸿蒙实战】从零开发「随机决策器」——选择困难症终结者
华为·harmonyos
国霄11 小时前
从编译产物看懂 ArkUI V2 `@BuilderParam` 的反应式陷阱
harmonyos
再见65815 小时前
鸿蒙Next实战开发(四):个人中心与系统设置页面开发
华为·harmonyos
坚果派·白晓明16 小时前
[鸿蒙PC三方库移植适配] 使用 AtomCode + Skills 自动完成spdlog鸿蒙化适配
c++·华为·ai编程·harmonyos·skills·atomcode
不爱学英文的码字机器16 小时前
[鸿蒙PC命令行移植适配]移植rust三方库sd到鸿蒙PC的完整实践
华为·rust·harmonyos
烛衔溟17 小时前
HarmonyOS 基础 UI 构建 —— 组件、布局与沉浸式效果
ui·华为·harmonyos
不爱吃糖的程序媛17 小时前
React Native 三方库 react-native-share 的 HarmonyOS 适配实战
react native·react.js·harmonyos