我有一个Map<String, List<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:用于填充多值地图的功能
function populateMap(map, k, v) {
map[k] = map[k] || [];
map[k].push(v);
}
要点3:填充地图
//映射键是网页中各种属性的连接,以 |
populateMap(map, 'release|attachment|license1', value1);
populateMap(map, 'release|attachment|license1', value2);
populateMap(map, 'release1|attachment1|license1', value1);
populateMap(map, 'release1|attachment1|license2', value2);
populateMap(map, 'release1|attachment1|license3', value3);
populateMap(map, 'release2|attachment2|license2', value1);
populateMap(map, 'release2|attachment2|license2', value2);
第4点:迭代地图并填充jsonObject
for (var i in map) {
var keys = i.split('|'), // splitting keys based on |
last = keys.pop(),
values = map[i];
values.forEach(function (item, index) {
keys.reduce((r, a) => r[a] = r[a] || {}, jsonObject)[last] = item;
});
}
第5点:当前输出(在控制台上打印jsonObject)
{
"release": {
"attachment": {
"license1": "[Object]"
}
},
"release1": {
"attachment1": {
"license1": "[Object]",
"license2": "[Object]",
"license3": "[Object]"
}
},
"release2": {
"attachment2": {
"license2": "[Object]"
}
}
}
第6点:预期输出(jsonObject)
{
"release": {
"attachment": {
"license1": "[Object, Object]" // expecting array of objects here ^^
}
},
"release1": {
"attachment1": {
"license1": "[Object]",
"license2": "[Object]",
"license3": "[Object]"
}
},
"release2": {
"attachment2": {
"license2": "[Object, Object]"
}
}
}
^^由于map的键具有对象数组,因此我想在jsonObject
中具有对象数组。
有人需要帮我做一下调整,以第4点遍历map
以获得预期的结果吗?
答案 0 :(得分:1)
您使用.set
,.get
,.has
和.delete
之类的方法与Map
进行交互;将其元素设置为属性是不正确的。
function populateMap(map, k, v) {
let values = map.get(k);
if (values === undefined) {
map.set(k, values = []);
}
values.push(v);
}
然后遍历地图,如下所示:
const jsonObject = {};
for (const [key, values] of map) {
setPath(jsonObject, key.split('|'), values);
}
和您的setPath
实现几乎相同:
function setPath(obj, [...keys], item) {
const last = keys.pop();
keys.reduce((r, a) => r[a] = r[a] || {}, obj)[last] = item;
}
toto:
function populateMap(map, k, v) {
let values = map.get(k);
if (values === undefined) {
map.set(k, values = []);
}
values.push(v);
}
function setPath(obj, [...keys], item) {
const last = keys.pop();
keys.reduce((r, a) => r[a] = r[a] || {}, obj)[last] = item;
}
const map = new Map();
populateMap(map, 'release|attachment|license1', 'value1');
populateMap(map, 'release|attachment|license1', 'value2');
populateMap(map, 'release1|attachment1|license1', 'value1');
populateMap(map, 'release1|attachment1|license2', 'value2');
populateMap(map, 'release1|attachment1|license3', 'value3');
populateMap(map, 'release2|attachment2|license2', 'value1');
populateMap(map, 'release2|attachment2|license2', 'value2');
const jsonObject = {};
for (const [key, values] of map) {
setPath(jsonObject, key.split('|'), values);
}
console.log(JSON.stringify(jsonObject));