[英]convert Map<String, Object> to json in javascript
我有一個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.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
擁有對象數組。
有人可以幫我調整需要在第3點的 setPath
函數中完成,以獲得預期的結果嗎?
PS:我知道我在這里問了類似的問題。
您正在覆蓋許可證陣列,而不是僅添加新項目。
改變這一行
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.