HarmonyOS 鸿蒙Next:hashmap如何转json格式

发布于 1周前 作者 caililin 来自 鸿蒙OS

HarmonyOS 鸿蒙Next:hashmap如何转json格式

hashmap如何转json格式
2 回复

核心代码如下:

let str: string = '';

for (let i = 0; i < channels.length; i++) {

  let obj5: Record<string, string> = {};

  channels[i].forEach((value, key) => {

    if (key != undefined && key != null) {

      obj5[key] = value;

    }

  })

  str += JSON.stringify(obj5) + ','
}

Logger.info("{" + '"channelMap":[' + str + "]}")

完整代码如下:

import { Logger } from '../commen.util/Logger';

@Entry
@Component
struct HuoBanDeHapToJson {

  aboutToAppear(): void {
    this.createBindJson()
  }

  createBindJson(): string {

    let channels: Array<Map<string, string>> = [];

    let channelCp: Map<string, string> = new Map();
    channelCp.set("name", "1013")
    channelCp.set("version", "1")

    let obj1: Record<string, string> = {};
    channelCp.forEach((value, key) => {

      if (key != undefined && key != null) {

        obj1[key] = value;

      }

    })

    let channelLittlePay: Map<string, string> = new Map();
    channelLittlePay.set("name", "01");
    channelLittlePay.set("version", "1");

    let obj2: Record<string, string> = {};
    channelLittlePay.forEach((value, key) => {

      if (key != undefined && key != null) {

        obj2[key] = value;

      }

    })

    let channelWeiChat: Map<string, string> = new Map();
    channelWeiChat.set("name", "02");
    channelWeiChat.set("version", "1");

    let obj3: Record<string, string> = {};
    channelWeiChat.forEach((value, key) => {

      if (key != undefined && key != null) {

        obj3[key] = value;

      }

    })

    let bill99: Map<string, string> = new Map();
    bill99.set("name", "04");
    bill99.set("version", "1");

    let obj4: Record<string, string> = {};
    bill99.forEach((value, key) => {

      if (key != undefined && key != null) {

        obj4[key] = value;

      }

    })

    channels.push(channelCp);
    channels.push(channelLittlePay);
    channels.push(channelWeiChat);
    channels.push(bill99);

    let channelMap: Map<string, Array<Map<string, string>>> = new Map();
    channelMap.set("channels", channels);

    let str: string = ''

    for (let i = 0; i < channels.length; i++) {

      let obj5: Record<string, string> = {};

      channels[i].forEach((value, key) => {

        if (key != undefined && key != null) {

          obj5[key] = value;

        }

      })

      str += JSON.stringify(obj5) + ','
    }

    Logger.info("{" + '"channelMap":[' + str + "]}")

    return JSON.stringify(channelMap);
  }

  build() {}
}

更多关于HarmonyOS 鸿蒙Next:hashmap如何转json格式的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS(鸿蒙)系统中,将HashMap转换为JSON格式通常依赖于第三方库或者系统自带的序列化工具。由于鸿蒙系统主要使用其自研的ArkUI框架和Dart语言进行开发,这里提供一个基于Dart语言的示例,展示如何将HashMap(在Dart中对应为Map)转换为JSON格式。

在Dart中,可以使用dart:convert库中的jsonEncode函数来实现这一转换。以下是一个简单的示例代码:

import 'dart:convert';

void main() {
  // 创建一个Map对象,模拟HashMap
  Map<String, dynamic> map = {
    'key1': 'value1',
    'key2': 123,
    'key3': true,
    'key4': [1, 2, 3],
    'key5': {'nestedKey': 'nestedValue'}
  };

  // 将Map转换为JSON字符串
  String jsonString = jsonEncode(map);

  // 打印JSON字符串
  print(jsonString);
}

上述代码创建了一个包含不同类型值的Map对象,并使用jsonEncode函数将其转换为JSON格式的字符串。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部