在JavaScript中将Map<String, Object>转换为JSON

3

我有一个 Map<String, Object> 对象,我想将其转换为 JSON 格式。

这里是我的代码:
步骤 1: 声明变量

var map = new Map(), //map
jsonObject= {}, // oject that needs to be converted to JSON
value1 = { topic: "someText1", status: "0", action: "1", comment: "someComment1" }, // values of Map
value2 = { topic: "someText2", status: "0", action: "0", comment: "someComment2" },
value3 = { topic: "someText3", status: "1", action: "1", comment: "someComment3" };

第二点:填充地图
// 地图的键是从网页中连接不同属性得到的,用|分隔

map.set('release|attachment|license1|topic1', value1);
map.set('release|attachment|license1|topic2', value1);
map.set('release1|attachment1|license1|topic1', value1);
map.set('release1|attachment1|license2|topic2', value2);
map.set('release1|attachment1|license3|topic3', value3);
map.set('release2|attachment2|license2|topic2', value2);
map.set('release2|attachment2|license2|topic3', value2);

第三点:遍历Map并填充JsonObject

for (const [key, values] of map) {
    setPath(jsonObject, key.split('|'), values);
}
function setPath(obj, [...keys], item) {
    keys.pop(); // removing topic
    const last = keys.pop();
    keys.reduce((r, a) => r[a] = r[a] || {}, obj)[last] =  [item];
}

第四点:当前输出 [console.log(JSON.stringify(jsonObject));]。

{
  "release": {
    "attachment": {
      "license1": [
        {
          "topic": "someText2",
          "status": "0",
          "action": "0",
          "comment": "someComment2"
        }
      ]
    }
  },
  "release1": {
    "attachment1": {
      "license1": [
        {
          "topic": "someText1",
          "status": "0",
          "action": "1",
          "comment": "someComment1"
        }
      ],
      "license2": [
        {
          "topic": "someText2",
          "status": "0",
          "action": "0",
          "comment": "someComment2"
        }
      ],
      "license3": [
        {
          "topic": "someText3",
          "status": "1",
          "action": "1",
          "comment": "someComment3"
        }
      ]
    }
  },
  "release2": {
    "attachment2": {
      "license2": [
        {
          "topic": "someText3",
          "status": "1",
          "action": "1",
          "comment": "someComment3"
        }
      ]
    }
  }
}

第五点: 预期输出(jsonObject)

{
  "release": {
    "attachment": {
      "license1": [
        {
          "topic": "someText1", // ^^ This object is missing in current output.
          "status": "0",
          "action": "1",
          "comment": "someComment1"
        },
        {
          "topic": "someText2",
          "status": "0",
          "action": "0",
          "comment": "someComment2"
        }
      ]
    }
  },
  "release1": {
    "attachment1": {
      "license1": [
        {
          "topic": "someText1",
          "status": "0",
          "action": "1",
          "comment": "someComment1"
        }
      ],
      "license2": [
        {
          "topic": "someText2",
          "status": "0",
          "action": "0",
          "comment": "someComment2"
        }
      ],
      "license3": [
        {
          "topic": "someText3",
          "status": "1",
          "action": "1",
          "comment": "someComment3"
        }
      ]
    }
  },
  "release2": {
    "attachment2": {
      "license2": [
        {
          "topic": "someText2", // ^^ This object is missing in current output.
          "status": "0",
          "action": "0",
          "comment": "someComment2"
        },
        {
          "topic": "someText3",
          "status": "1",
          "action": "1",
          "comment": "someComment3"
        }
      ]
    }
  }
}

^^我希望在jsonObject中拥有对象数组。

有人可以帮我调整setPath函数中第三点需要进行的修改,以获得预期结果吗?

PS:我知道我在这里问过类似的问题。


1
你确定要执行两次 map.set('release|attachment|license1|topic1', value1); 吗? - Bergi
1个回答

2
你正在覆盖许可证数组,而不是仅附加新项目。
将此行更改为:
keys.reduce((r, a) => r[a] = r[a] || {}, obj)[last] =  [item];

to this

const target = keys.reduce((r, a) => r[a] = r[a] || {}, obj);
(target[last] = target[last] || []).push(item);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接