简体   繁体   English

转换地图 <String, Object> 在javascript中使用json

[英]convert Map<String, Object> to json in javascript

I have a Map<String, Object> and I want to convert it to JSON . 我有一个Map<String, Object> ,我想将它转换为JSON

Here is what I have: 这是我有的:
Point 1: Declaring variables 第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" };

Point 2: populating Map 第2点:填充地图
// key of map is concatenation of various attributes from web page, separated by | //映射的键是来自网页的各种属性的串联,由|分隔

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

Point 3: iterating map and populating jsonObject 第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];
}

Point 4: current output [ console.log(JSON.stringify(jsonObject)); 第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"
        }
      ]
    }
  }
}

Point 5: expected output (jsonObject) 第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"
        }
      ]
    }
  }
}

^^ I want to have array of objects in jsonObject . ^^我想在jsonObject拥有对象数组。

Can someone help me with tweak needs to be done in setPath function in Point 3 to obtain expected result? 有人可以帮我调整需要在第3点的 setPath函数中完成,以获得预期的结果吗?

PS: I know that I have asked similar question here . PS:我知道我在这里问了类似的问题。

You are overwriting the license arrays instead of just appending the new items. 您正在覆盖许可证阵列,而不是仅添加新项目。

Change this line 改变这一行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM