将多值Map< String,Object [ ]>转换为JavaScript中的json

jjjwad0x  于 2023-06-25  发布在  Java
关注(0)|答案(2)|浏览(110)

我有一个Map<String, List<Object>>,我想把它转换成JSON
这是我的:

    • 第一点:**声明变量
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
function populateMap(map, k, v) {
    map[k] = map[k] || [];
    map[k].push(v);
}
    • 第三点:**填充Map
  • //map的key是web页面中各种属性的拼接,用分隔|*
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);
    • 第四点:**迭代map并填充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;
    });
}
    • 第五点:**当前输出(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
有人可以帮助我调整需要在Point 4map遍历中做的调整来获得预期的结果吗?

r8xiu3jd

r8xiu3jd1#

您可以使用.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);
}

然后遍历map看起来像这样:

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

总人数:

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

camsedfj2#

可以使用对象Map器将多值Map解析为对象或JSON
new ObjectMapper.writeValueAsString(requestValue.toSingleValueMap())

相关问题