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
4天前
浏览
收藏 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
微信
回复
4天前
相关问题
HarmonyOS JSON怎么Map?
56浏览 • 1回复 待解决
如何实现MapJSON字符串
970浏览 • 1回复 待解决
如何JSON字符串Class对象
301浏览 • 1回复 待解决
HarmonyOS 加载.json格式的动图资源
301浏览 • 1回复 待解决
HarmonyOS Uint8Array格式字符串的方法
517浏览 • 1回复 待解决
如何JSON字符串Class对象数组?
329浏览 • 1回复 待解决
HarmonyOS使用HashMap崩溃
253浏览 • 1回复 待解决
能否支持json实体类的工具
957浏览 • 1回复 待解决
MapJSON,哪位大佬知道怎么解决啊?
1620浏览 • 0回复 待解决
HarmonyOS array 如何 arraylist ?
63浏览 • 1回复 待解决
HarmonyOS Record HashMap map类型选择
585浏览 • 2回复 待解决
如何将Map/HashMap转为Object对象?
1619浏览 • 1回复 待解决
HarmonyOS string number
612浏览 • 1回复 待解决
router传递hashmap参数问题
1449浏览 • 1回复 待解决