代码之家  ›  专栏  ›  技术社区  ›  Abhiz

如何在angular 5中将从API接收到的数据转换为JSON格式

  •  0
  • Abhiz  · 技术社区  · 6 年前

    我的目标是用angular5显示所有的数据。

    {
     "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"
     }
    

    这只是一个数据样本。在我的程序中,我将在没有任何正确模式的情况下动态地获取这些对象。我必须显示所有这些键值对。如何动态检索所有嵌套的键值对?

    检索以下类型的键值对不是问题,而是 事件\u值 物体是最具挑战性的部分。因为如果这样的数据将通过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",
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   user1859022 Saurabh Solanki    6 年前

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

    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

        2
  •  0
  •   Yanis-git    6 年前

    我不知道你到目前为止想达到什么目的。我假设您有非结构化的Json输入,您希望扫描它,并保留列出的键的所有值。

    如果我是对的,下面的代码可以完成这项工作:

    import {data} from './data';
    import {isObject, isArray, isNullOrUndefined} from 'util';
    
    
    const haveToGoDeeper = val => isObject(val) || isArray(val);
    
    const parser = val => {
      const info = [];
      let finded = {};
      for(let index in val) {
        ["is_false_positive","event_at","source","resolution","url","condition","event_id","event_type","matched_at"].forEach(k => {
            //If actual keys is on the list.
            if(k === index)
                finded[index] = val[index];
        });
        // If we have find at least 1 items on bellow array of keys, we keep it.
        if(Object.keys(finded).length > 0)
          info.push(finded);
    
        //Recursive section.
        if(haveToGoDeeper(val[index])) {
            info.push(...parser(val[index]));
        }
    
      }
    
      return info;
    };
    
    
    console.log(parser(data).filter(e => e !== null));
    

    Online Sample

    如果不是这个,请评论,我很高兴更新我的帖子。