繁体   English   中英

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

[英]How to convert data received from API to JSON format in angular 5

我的目标是使用角度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);

如果确实是任意键值列表,则可能还需要考虑嵌套数组。

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

我不确定您到目前为止想要达到的目标。 我假设您具有非结构化的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));

在线样本

如果不是这一条,请发表评论,我很乐意更新我的帖子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM