HarmonyOS JSON中的map结构如何序列化

例如, JSON.stringify(InfoData) 只能把实体类 InfoData 的 成员属性转成 JSON,如果 InfoData 中多层嵌套了 InfoDataB 的引用 InfoDataB 嵌套 InfoDataC 要怎么转出完整的json结构?

HarmonyOS
2024-08-28 14:30:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
FengTianYa

Map不支持序列化,不能通过JSON.stringify接口返回JSON字符串。需要将Map先转Object类型,再通过JSON.stringify序列化。

可以参考以下方式将Map序列化:

1、Map先转Record类型,再通过JSON.stringify序列化。

// 递归,以防value也是Map 
function  Map2Rec(map:Map<string, ESObject>):Record<string,ESObject>{ 
  let Rec:Record<string,ESObject> = {}; 
  map.forEach((value:ESObject, key:string) => { 
    if(value instanceof Map){ 
      let vRec:Record<string,ESObject> = Map2Rec(value); 
      value = vRec 
    } 
    Rec[key] = value 
  }) 
  return Rec 
} 
let myRec:Record<string,ESObject> = Map2Rec(map) 
let str:string = JSON.stringify(myRec) 
console.info(str)

2、Map先转Object类型,再通过JSON.stringify序列化。

function Map2Obj(map:Map<string, ESObject>):object{ 
  let obj:object = new Object() 
  map.forEach((value:ESObject, key:string) => { 
    if(value instanceof Map){ 
      let vObj:object = Map2Obj(value); 
      value = vObj 
    } 
    obj[key] = value 
  }) 
  return obj 
}

3、TS文件中封装Object.fromEntries

// ts 
export class ObjectUtil { 
  static ObjectFromEntries(source: Iterable<readonly any[]>): Object { 
    return Object.fromEntries(source) 
  } 
} 
 
//ets 
let str:string = JSON.stringify(ObjectUtil.ObjectFromEntries(map)) 
console.info(str)

可以使用以下方式序列化实体类中的map:

function class2Obj(esObj:ESObject):object{ 
  let obj:object = new Object() 
  Object.keys(esObj).forEach((value:ESObject) => { 
    if(esObj[value] instanceof Map){ 
      let vObj:object = Map2Obj(esObj[value]); 
      obj[value] = vObj 
    } 
    else if(typeof esObj[value] === "object"){ 
      let vObj:object = class2Obj(esObj[value]); 
      obj[value] = vObj 
    } 
    else 
      obj[value] = esObj[value] 
  }) 
  return obj 
} 
 
function Map2Obj(map:Map<string, ESObject>):object{ 
  let obj:object = new Object() 
  map.forEach((value:ESObject, key:string) => { 
    if(value instanceof Map){ 
      let vObj:object = Map2Obj(value); 
      value = vObj 
    } 
    obj[key] = value 
  }) 
  return obj 
} 
function class2Obj(esObj:object):object{ 
  let obj:object = new Object() 
  Object.keys(esObj).forEach((value:ESObject) => { 
    if(esObj[value] instanceof HashMap){ 
      let vObj:object = Map2Obj(esObj[value]); 
      obj[value] = vObj 
    } 
    else if(esObj[value] instanceof Array){ 
      let vObj:object = Array2Obj(esObj[value]); 
      obj[value] = vObj 
    } 
    else if(typeof esObj[value] === "object"){ 
      let vObj:object = class2Obj(esObj[value]); 
      obj[value] = vObj 
    } 
    else 
      obj[value] = esObj[value] 
  }) 
  return obj 
} 
 
 
function Map2Obj(map:HashMap<string, object>):object{ 
  let obj:object = new Object() 
  map.forEach((value:object, key:string) => { 
    if(value instanceof HashMap){ 
      value = Map2Obj(value); 
    } 
    else if(value instanceof Array){ 
      value = Array2Obj(value); 
    } 
    else if(typeof value === "object"){ 
      value = class2Obj(value); 
    } 
    obj[key] = value 
  }) 
  return obj 
} 
 
function Array2Obj(array:Array<Object>):object{ 
  let arr:Array<Object> = new Array() 
  array.forEach((value:Object) => { 
    if(value instanceof HashMap){ 
      let vObj:object = Map2Obj(value); 
      arr.push(vObj) 
    } 
    else if(value instanceof Array){ 
      let vObj:object = Array2Obj(value); 
      arr.push(vObj) 
    } 
    else if(typeof value === "object"){ 
      let vObj:object = class2Obj(value); 
      arr.push(vObj) 
    } 
    else 
      arr.push(value) 
  }) 
  return arr 
}
分享
微博
QQ
微信
回复1
2024-08-28 23:22:29
相关问题
JSON序列化如何重命名字段?
217浏览 • 1回复 待解决
求大佬告知如何序列化对象
689浏览 • 1回复 待解决
有谁知道 task 参数无法序列化
1773浏览 • 1回复 待解决
自定义类如何实现序列化
1951浏览 • 1回复 待解决
HarmonyOS json数据如何转成map
907浏览 • 1回复 待解决
如何在ArkTS实现JSONMap转换?
3092浏览 • 1回复 待解决
HarmonyOS JSON怎么转Map
261浏览 • 1回复 待解决
HarmonyOS json对象转map类型
127浏览 • 1回复 待解决