如何从 SNMP 设备迭代对象并使用预定义规则创建可读输出
How to iterate over an object from an SNMP device and create a readable output using predefined rules
我在 SNMP 设备中有以下 JSON:
mib = [
"1.3.6.1.2.1.43.11.1": {
"1.1": {
"3": "1",
"5": "3",
"6": "Cyan Toner Cartridge, WorkCentre 6505N",
"8": "2000",
"9": "800"
},
"1.2": {
"3": "2",
"5": "3",
"6": "Magenta Toner Cartridge, WorkCentre 6505N",
"8": "1000",
"9": "400"
},
"1.5": {
"3": "0",
"5": "9",
"6": "Imaging Unit, WorkCentre 6505N",
"8": "24000",
"9": "24000"
}
},
"1.3.6.1.2.1.43.12.1": {
"1.1": {
"4": "cyan"
},
"1.2": {
"4": "magenta"
}
}
]
我想要的结果是这样的:
device["markerSupplies"]: [
0: {
color: "cyan",
type: "toner",
description: "Cyan Toner Cartridge, WorkCentre 6505N",
capacity: "2000",
value: "800"
},
1: {
color: "magenta",
type: "toner",
description: "Magenta Toner Cartridge, WorkCentre 6505N",
capacity: "1000",
value: "400"
},
2: {
color: "",
type: "opc",
description: "Imaging Unit, WorkCentre 6505N",
capacity: "24000",
value: "24000"
},
]
“1.1”、“1.2”... 只是索引,我了解其中的内容。
每一个 属性 在他们里面称为一个列并对应于它的索引。
我知道每个table的列:
1.3.6.1.2.1.43.11.1
3 the color index inside 1.3.6.1.2.1.43.12.1
5
3 "toner"
9 "opc"
6 description
8 capacity
9 level
1.3.6.1.2.1.43.12.1
4 color name
我如何创建一个 JSON 信息对象,使用 javascript 代码我可以从设备迭代 JSON
并创建我上面显示的输出结果?
您可以为颜色和类型使用一些辅助变量,并迭代键以构建新数组。
var mib = { "1.3.6.1.2.1.43.11.1": { "1.1": { 3: "1", 5: "3", 6: "Cyan Toner Cartridge, WorkCentre 6505N", 8: "2000", 9: "800" }, "1.2": { 3: "2", 5: "3", 6: "Magenta Toner Cartridge, WorkCentre 6505N", 8: "1000", 9: "400" }, "1.5": { 3: "0", 5: "9", 6: "Imaging Unit, WorkCentre 6505N", 8: "24000", 9: "24000" } }, "1.3.6.1.2.1.43.12.1": { "1.1": { 4: "cyan" }, "1.2": { 4: "magenta" } } },
cols = { 3: 'color', 5: 'type', 6: 'description', 8: 'capacity', 9: 'level' },
types = { 3: 'toner', 9: 'opc' },
markerSupplies = mib['1.3.6.1.2.1.43.11.1'],
colors = mib['1.3.6.1.2.1.43.12.1'],
result = Object.keys(markerSupplies).map(function (k) {
var o = {};
Object.keys(cols).forEach(function (c) {
if (c === '3') {
o[cols[c]] = (colors[k] || {})['4'] || '';
return;
}
if (c === '5') {
o[cols[c]] = types[markerSupplies[k][c]] || '';
return;
}
o[cols[c]] = markerSupplies[k][c] || '';
});
return o;
});
console.log(result);
我在 SNMP 设备中有以下 JSON:
mib = [
"1.3.6.1.2.1.43.11.1": {
"1.1": {
"3": "1",
"5": "3",
"6": "Cyan Toner Cartridge, WorkCentre 6505N",
"8": "2000",
"9": "800"
},
"1.2": {
"3": "2",
"5": "3",
"6": "Magenta Toner Cartridge, WorkCentre 6505N",
"8": "1000",
"9": "400"
},
"1.5": {
"3": "0",
"5": "9",
"6": "Imaging Unit, WorkCentre 6505N",
"8": "24000",
"9": "24000"
}
},
"1.3.6.1.2.1.43.12.1": {
"1.1": {
"4": "cyan"
},
"1.2": {
"4": "magenta"
}
}
]
我想要的结果是这样的:
device["markerSupplies"]: [
0: {
color: "cyan",
type: "toner",
description: "Cyan Toner Cartridge, WorkCentre 6505N",
capacity: "2000",
value: "800"
},
1: {
color: "magenta",
type: "toner",
description: "Magenta Toner Cartridge, WorkCentre 6505N",
capacity: "1000",
value: "400"
},
2: {
color: "",
type: "opc",
description: "Imaging Unit, WorkCentre 6505N",
capacity: "24000",
value: "24000"
},
]
“1.1”、“1.2”... 只是索引,我了解其中的内容。 每一个 属性 在他们里面称为一个列并对应于它的索引。
我知道每个table的列:
1.3.6.1.2.1.43.11.1
3 the color index inside 1.3.6.1.2.1.43.12.1
5
3 "toner"
9 "opc"
6 description
8 capacity
9 level
1.3.6.1.2.1.43.12.1
4 color name
我如何创建一个 JSON 信息对象,使用 javascript 代码我可以从设备迭代 JSON 并创建我上面显示的输出结果?
您可以为颜色和类型使用一些辅助变量,并迭代键以构建新数组。
var mib = { "1.3.6.1.2.1.43.11.1": { "1.1": { 3: "1", 5: "3", 6: "Cyan Toner Cartridge, WorkCentre 6505N", 8: "2000", 9: "800" }, "1.2": { 3: "2", 5: "3", 6: "Magenta Toner Cartridge, WorkCentre 6505N", 8: "1000", 9: "400" }, "1.5": { 3: "0", 5: "9", 6: "Imaging Unit, WorkCentre 6505N", 8: "24000", 9: "24000" } }, "1.3.6.1.2.1.43.12.1": { "1.1": { 4: "cyan" }, "1.2": { 4: "magenta" } } },
cols = { 3: 'color', 5: 'type', 6: 'description', 8: 'capacity', 9: 'level' },
types = { 3: 'toner', 9: 'opc' },
markerSupplies = mib['1.3.6.1.2.1.43.11.1'],
colors = mib['1.3.6.1.2.1.43.12.1'],
result = Object.keys(markerSupplies).map(function (k) {
var o = {};
Object.keys(cols).forEach(function (c) {
if (c === '3') {
o[cols[c]] = (colors[k] || {})['4'] || '';
return;
}
if (c === '5') {
o[cols[c]] = types[markerSupplies[k][c]] || '';
return;
}
o[cols[c]] = markerSupplies[k][c] || '';
});
return o;
});
console.log(result);