将数组转换成Map对象,然后调用let str = JSON.stringify(newMap),将Map转换成字符串,转换出来的结果是{}
Api 11 以上 Object.fromEntries不可用
【解决方案】
export class JsonUtil {
//json字符串转为map
static jsonParseMap(jsonStr: string) {
let jsonObj: Record<string, Object> = JSON.parse(jsonStr);
let newMap: Map<string, object> = new Map<string, object>(Object.entries(jsonObj));
return newMap;
}
//map转为json字符串
static mapParseJson(map: Map<string, Object>) {
let obj: Record<string, Object> = {};
map.forEach((value, key) => {
if (key != undefined && key != null) {
obj[key] = value;
}
})
let jsonStr = json.stringify(obj)
return jsonStr;
}
}
标签:jsonStr,Map,map,json,HarmonyOS,JSON,let,key
From: https://blog.csdn.net/wu1004019946/article/details/139291200