我有一个JSON文件,其中每个JSON对象都有一个唯一的标识符( IP地址)
给定IP地址范围时,我想根据给定范围过滤原始JSON并将其存储在新文件中。 (即IP地址在给定范围内的所有JSON对象)
JSON文件:
[{
"Name": "SERVER1",
"ipv4Address": "192.168.0.50",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER2",
"ipv4Address": "192.168.0.51",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER3",
"ipv4Address": "192.168.0.52",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER4",
"ipv4Address": "192.168.0.53",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER5",
"ipv4Address": "192.168.0.54",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
}]
如果给定范围为192.168.0.52-192.168.0.54,则输出应为:
[{
"Name": "SERVER3",
"ipv4Address": "192.168.0.52",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER4",
"ipv4Address": "192.168.0.53",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER5",
"ipv4Address": "192.168.0.54",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
}]
答案 0 :(得分:1)
使用以下代码段。
function isWithinRange(ip, lowerBound, upperBound) {
// Put all IPs into one array for iterating and split all into their own
// array of segments
var ips = [ip.split('.'), lowerBound.split('.'), upperBound.split('.')];
// Convert all IPs to ints
for(var i = 0; i < ips.length; i++) {
// Typecast all segments of all ips to ints
for(var j = 0; j < ips[i].length; j++) {
ips[i][j] = parseInt(ips[i][j]);
}
// Bit shift each segment to make it easier to compare
ips[i] =
(ips[i][0] << 24) +
(ips[i][1] << 16) +
(ips[i][2] << 8) +
(ips[i][3]);
}
// Do comparisons
if(ips[0] >= ips[1] && ips[0] <= ips[2]) return true;
return false;
}
var json = [{
"Name": "SERVER1",
"ipv4Address": "192.168.0.50",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER2",
"ipv4Address": "192.168.0.51",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER3",
"ipv4Address": "192.168.0.52",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER4",
"ipv4Address": "192.168.0.53",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
},
{
"Name": "SERVER5",
"ipv4Address": "192.168.0.54",
"OperatingSystem": [],
"OperatingSystemServicePack": null,
"OperatingSystemVersion": "6.3 (9600)"
}];
var newArr = [];
var lowerBound = '192.168.0.52';
var upperBound = '192.168.0.54';
json.forEach(function(item){
var isInRange = isWithinRange(item.ipv4Address, lowerBound, upperBound);
if(isInRange){
newArr.push(item);
}
});
console.log("new Array : ", newArr);
使用@SaltedBlowfish通过他的Bit Shifting
公式提供的解决方案