javascript - 如何将从 API 接收到的数据转换为角度 5 中的 JSON 格式
问题描述
我的目标是使用 angular 5 显示所有数据。
{
"route": "/hx/api/v3/alerts/id",
"data": {
"agent": {
"containment_state": "normal",
"_id": "Aq0mOZ2D9ubcBwkoB9riaX",
"url": "/hx/api/v3/hosts/Aq0mOZ2D9ubcBwkoB9riaX"
},
"reported_at": "2018-08-31T20:51:59.903Z",
"matched_source_alerts": [
],
"is_false_positive": false,
"event_at": "2018-08-31T20:51:59.496Z",
"source": "MAL",
"resolution": "ALERT",
"url": "/hx/api/v3/alerts/3271",
"condition": null,
"event_id": null,
"event_type": null,
"matched_at": "2018-08-31T20:51:59.496Z",
"event_values": {
"scanned-object": {
"file-event": {
"actor-process": {
"path": "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe",
"pid": "12364",
"user": {
"domain": "HCCC-MANAGER1",
"username": "admin"
}
},
"file-path": "C:\\Users\\admin\\AppData\\Local\\Microsoft\\Windows\\INetCache\\Low\\IE\\2UK27Y9J\\ACY8PW37.htm"
},
"scanned-object-type": "file-event"
},
"scan-type": "oas",
"system-data": {
"engine-version": "11.0.1.18",
"content-version": "7.77212",
"xmlns": "http://www.fireeye.com/antimalware-alert",
"whitelist-schema-version": "1.0.0",
"alert-version": "1",
"product-version": "26.35.0.0",
"correlation-id": "7a1d883b-e579-4d2a-b050-5eec7def16a2",
"xsi:schemaLocation": "http://www.fireeye.com/antimalware-alert AM-alert.xsd",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"whitelist-content-version": "1.1.6",
"timestamp": "2018-08-31T20:51:59.496Z"
},
"detections": {
"detection": [
{
"infection": {
"infection-type": "malware",
"infection-name": "JS:Trojan.Cryxos.1726",
"confidence-level": "high"
},
"infected-object": {
"object-type": "file",
"file-object": {
"container": "true",
"access-time": "2018-08-31T20:51:59.238Z",
"modification-time": "2018-08-31T20:51:59.238Z",
"sha1sum": "96c4c0c176933a58ad480cbd63d999ed11e0a968",
"md5sum": "9b4d577410c14dac4628f471ba85f344",
"creation-time": "2018-08-31T20:51:59.238Z",
"inner-file-path": "(INFECTED_JS)",
"size-in-bytes": "14100",
"packed": "false",
"file-path": "C:\\Users\\admin\\AppData\\Local\\Microsoft\\Windows\\INetCache\\Low\\IE\\2UK27Y9J\\ACY8PW37.htm"
}
},
"action": {
"result": "success",
"requested-action": "none",
"reboot-required": "false",
"applied-action": "none",
"error": "0",
"actioned-object": {
"object-type": "file",
"file-object": {
"container": "true",
"access-time": "2018-08-31T20:51:59.238Z",
"modification-time": "2018-08-31T20:51:59.238Z",
"sha1sum": "96c4c0c176933a58ad480cbd63d999ed11e0a968",
"md5sum": "9b4d577410c14dac4628f471ba85f344",
"creation-time": "2018-08-31T20:51:59.238Z",
"inner-file-path": "(INFECTED_JS)",
"size-in-bytes": "14100",
"packed": "false",
"file-path": "C:\\Users\\admin\\AppData\\Local\\Microsoft\\Windows\\INetCache\\Low\\IE\\2UK27Y9J\\ACY8PW37.htm"
}
}
}
}
]
}
},
"_id": 3271
},
"details": [
],
"message": "OK"
}
这只是一个数据样本。在我的程序中,我将在没有任何适当模式的情况下动态获取此类对象。我必须展示所有这些键值对。如何动态检索所有嵌套的键值对?
检索以下类型的键值对不是问题,但event_values对象是最具挑战性的部分。因为如果这些数据将通过 API 动态获取,如何检索它?
"is_false_positive": false,
"event_at": "2018-08-31T20:51:59.496Z",
"source": "MAL",
"resolution": "ALERT",
"url": "/hx/api/v3/alerts/3271",
"condition": null,
"event_id": null,
"event_type": null,
"matched_at": "2018-08-31T20:51:59.496Z",
解决方案
不能完全确定您要做什么,但如果我理解正确,您应该能够像这样迭代对象:
function inspect(obj, depth){
for (let key in obj) {
var indent = new Array(depth * 3).join(' ')
if(typeof(obj[key]) === 'object')
inspect(obj[key], depth + 1)
else
console.log(indent + key + ":" + obj[key]);
}
}
inspect(event_values, 0);
如果它真的是一个任意键值列表,您可能还需要考虑嵌套数组。
推荐阅读
- node.js - NestJS 异步等待要求
- javascript - Chart.js 生成的图表具有完全不同的坐标
- flutter - 在文本溢出 Flutter 上创建页面
- wordpress - 使优惠券在woocommerce中提高产品价格
- django-rest-framework - 如何根据嵌套序列化程序中的数组获取主模型中的多条记录?
- git - 如何克隆远程 git 存储库,以便下载所有分支
- r - 在 R 中使用 ggplot2 在图中添加注释
- java - 比较两个不同的 MongoDB 集合 json 节点以进行验证
- pytorch - 在 PyTorch 中压缩两个连续维度
- c - C语言的注册和登录系统