鸿蒙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);
    }
  }
}
相关推荐
写雨.012 小时前
鸿蒙定位开发服务
华为·harmonyos·鸿蒙
goto_w17 小时前
uniapp上使用webview与浏览器交互,支持三端(android、iOS、harmonyos next)
android·vue.js·ios·uni-app·harmonyos
别说我什么都不会1 天前
ohos.net.http请求HttpResponse header中set-ccokie值被转成array类型
网络协议·harmonyos
码是生活1 天前
鸿蒙开发排坑:解决 resourceManager.getRawFileContent() 获取文件内容为空问题
前端·harmonyos
鸿蒙场景化示例代码技术工程师1 天前
基于Canvas实现选座功能鸿蒙示例代码
华为·harmonyos
小脑斧爱吃鱼鱼1 天前
鸿蒙项目笔记(1)
笔记·学习·harmonyos
鸿蒙布道师1 天前
鸿蒙NEXT开发对象工具类(TS)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
zhang1062091 天前
HarmonyOS 基础组件和基础布局的介绍
harmonyos·基础组件·基础布局
马剑威(威哥爱编程)1 天前
在HarmonyOS NEXT 开发中,如何指定一个号码,拉起系统拨号页面
华为·harmonyos·arkts
GeniuswongAir1 天前
Flutter极速接入IM聊天功能并支持鸿蒙
flutter·华为·harmonyos