Javascript 如何在angular 5中将从API接收的数据转换为JSON格式
我的目标是使用angular 5显示所有数据Javascript 如何在angular 5中将从API接收的数据转换为JSON格式,javascript,angular,Javascript,Angular,我的目标是使用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", "ma
{
"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",
到目前为止,我不确定你是否理解你试图实现的目标。我假设您有非结构化的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));
如果不是这个,请发表评论,我很乐意更新我的帖子。不能确定你想做什么,但如果我理解正确,你应该能够像这样迭代对象:
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);
如果它实际上是一个任意的键值列表,那么您也可能需要考虑嵌套数组。 例如:
非常感谢您的努力。但在这里,您将以下值作为键[“is_false_正值”、“event_at”、“source”、“resolution”、“url”、“condition”、“event_id”、“event_type”、“matched_at”]在我提供的对象中作为示例,这些键只包含一个值。但类键事件_值包含难以检索的嵌套对象。这种嵌套对象是完全动态的,因为它依赖于服务器端程序员。我的目标是检索对象中所有可用的键:值对并将其打印出来。谢谢。您能描述一下创建数组的原因吗?您指的是
事件值吗?如果是这样的话,那就是我创建的一个对象,用来模拟您将从api结果反序列化的对象(您看过示例代码了吗?),是的,我已经完成了它,并在代码中部署了它。。但是我仍然被困在一个点上,我无法得到父对象。例如,这是结果数组的一小部分<代码>1:pid:12364“2:“域:HCCC-MANAGER1”3:“用户名:admin”4:“文件路径:C:\Users\INetCache\Low\IE\2UK27Y9J\ACY8PW37.htm”5:“扫描对象类型:文件事件”6:“扫描类型:oas”“
key:value对被正确地提到,但是在我们得到这对的对象中找不到。这是对象的实际路径->扫描对象->文件事件->actor processpid:12364
但是我们只得到actor processpid:12364
我问你为什么创建这个数组`var indent=new array(depth*3)。join(“”)`如果不使用这个缩进,我也可以检索所有的key:value pairoh sry-你不需要这个arary-它只是用空格缩进上面代码中的控制台输出(我忘了从实例中删除它)