在HarmonyOS开发中JSON.stringify一个hasmap的问题?

在HarmonyOS开发中JSON.stringify一个hasmap的问题?

let data = new HashMap<string, string>()    
data.set("action", "test")  
let text = JSON.stringify(data) 

输出一个text={}
由于我这边想添加多个并且不同的key和value,并返回json的字符串

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

阅读 502
1 个回答

你可以参考以下代码:

const map1: HashMap<string, string> = new HashMap(); 
map1.set("a", "avalue"); 
map1.set("b", "bValue"); 
const jsonObject: Record<string, Object> = {}; 
map1.forEach((value, key) => { 
  if (key != undefined && value != undefined) { 
    jsonObject[key] = value; 
  } 
}) 
const jsonString = JSON.stringify(jsonObject) 

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进