首页 > 解决方案 > 如何将从 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",

标签: javascriptangular

解决方案


不能完全确定您要做什么,但如果我理解正确,您应该能够像这样迭代对象:

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

如果它真的是一个任意键值列表,您可能还需要考虑嵌套数组。

示例: https ://stackblitz.com/edit/typescript-yzpdvj


推荐阅读