HarmonyOS hashmap如何转json格式?

hashmap转json对象异常,为{}。

function createBindJson(): string { 
  let channels: Array<HashMap<string, string>> = []; 
 
  let channelCp: HashMap<string, string> = new HashMap(); 
  channelCp.set("name", "1013") 
  channelCp.set("version", "1") 
  LogUtils.d(BankCardUpgradePage.tag, JSON.stringify(channelCp) + "---1"); 
 
  let channelLittlePay: HashMap<string, string> = new HashMap(); 
  channelLittlePay.set("name", "01"); 
  channelLittlePay.set("version", "1"); 
 
  let channelWeiChat: HashMap<string, string> = new HashMap(); 
  channelWeiChat.set("name", "02"); 
  channelWeiChat.set("version", "1"); 
 
  let bill99: HashMap<string, string> = new HashMap(); 
  bill99.set("name", "04"); 
  bill99.set("version", "1"); 
 
  channels.push(channelCp); 
  channels.push(channelLittlePay); 
  channels.push(channelWeiChat); 
  channels.push(bill99); 
 
  LogUtils.d(BankCardUpgradePage.tag, JSON.stringify(channels) + "---22222"); 
 
  let channelMap: HashMap<string, Array<HashMap<string, string>>> = new HashMap(); 
  channelMap.set("channels", channels); 
 
  return JSON.stringify(channelMap); 
}

map转json,请给出封装的方法或者解决方案。

HarmonyOS
2024-11-04 09:58:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

核心代码如下:

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() { 
  } 
}
分享
微博
QQ
微信
回复
2024-11-04 15:59:42
相关问题
HarmonyOS JSON怎么Map?
324浏览 • 1回复 待解决
HarmonyOS mapjson对象
42浏览 • 1回复 待解决
HarmonyOS JSON对象失败
58浏览 • 1回复 待解决
HarmonyOS json对象map类型
184浏览 • 1回复 待解决
HarmonyOS JSON实体类
42浏览 • 1回复 待解决
HarmonyOS 关于JSON模问题
27浏览 • 1回复 待解决
如何实现MapJSON字符串
1146浏览 • 1回复 待解决
HarmonyOS 关于jsonmodel的处理
28浏览 • 1回复 待解决
如何JSON字符串Class对象
489浏览 • 1回复 待解决
如何JSON字符串Class对象数组?
502浏览 • 1回复 待解决
HarmonyOS 加载.json格式的动图资源
506浏览 • 1回复 待解决
HarmonyOS 是否有json实体类的工具
33浏览 • 1回复 待解决
MapJSON,哪位大佬知道怎么解决啊?
1782浏览 • 0回复 待解决
能否支持json实体类的工具
1149浏览 • 1回复 待解决
HarmonyOS Uint8Array格式字符串的方法
869浏览 • 1回复 待解决
HarmonyOS使用HashMap崩溃
420浏览 • 1回复 待解决