javascript - 在 javascript 中将 Map<String, Object> 转换为 json

标签 javascript json

我有一个 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" };

第 2 点:填充 map
//map的key是网页中各种属性的拼接,用|

分隔
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);

第 3 点: 迭代 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];
}

第 4 点电流输出 [ 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"
        }
      ]
    }
  }
}

第 5 点预期输出 (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 中完成吗?在第 3 点中发挥作用以获得预期结果?

PS:我知道我问过类似的问题here .

最佳答案

您正在覆盖许可证数组,而不是仅仅附加新项目。

改变这一行

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

对此

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

关于javascript - 在 javascript 中将 Map<String, Object> 转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57252151/

相关文章:

javascript - chart.js 流数据插件数据格式或配置错误

javascript - CodeMirror 最初是隐藏的

javascript - Rails 3 406 尝试使用 format.js 时出现 Not Acceptable 错误,也无法更改内容类型

ios - 在 Swift 中创建类和结构来表示 JSON 对象

jquery html 未在 getJSON 函数范围内传递回 html 变量问题

javascript - 单选按钮值显示在另一个表单上

javascript - 无法检测到匹配的空间

javascript - 通过AJAX(ASP.NET MVC)将json结果附加到div

json - 在 postgres、Django 中保存大型 json 对象

javascript - 使用标识符遍历 JSON 对象并获取带有标识符的特定值