【问题标题】:Parse Json data (from Azure)解析 Json 数据(来自 Azure)
【发布时间】:2020-09-11 09:28:31
【问题描述】:

我正在尝试解析此 json,并尝试获取一个表格,其中包含此数据中的列数和行数以及计算机名称。

{
  "alertId": "raxhighcpu",
  "Description": "",
  "NotificationType": "public",
  "LinkToSearchResults": "https://portal.azure.",
  "searchInterval": "00:05:00",
  "alertValue": "0",
  "alertName": "On-Premise CPU Alert",
  "IncludeSearchResults": true,
  "SearchQuery": "Perf\n| where _ResourceId == ''\n| where ObjectName == 'Processor' and CounterName contains 'Processor Time' and InstanceName == '_Total' and Computer in ((Perf | where CounterName == 'Processor Queue Length'| summarize AVGQUEUE = avg(CounterValue) by Computer| where AVGQUEUE>1)) | summarize AVGCPU = avg(CounterValue) by Computer\n| where AVGCPU>60\n| project Computer, AVGCPU",
  "SearchResults": {
    "tables": [
      {
        "name": "PrimaryResult",
        "columns": [
          {
            "name": "Computer",
            "type": "string"
          },
          {
            "name": "AVGCPU",
            "type": "real"
          }
        ],
        "rows": [
          [
            ".ci.corp",
            66.60977770487469
          ],
          [
            ".ci.corp",
            60.13546142578125
          ],
          [
            ".ci.corp",
            62.83857485453288
          ]
        ]
      }
    ],
    "statistics": {
      "query": {
        "executionTime": 0.640761,
        "resourceUsage": {
          "cache": {
            "memory": {
              "hits": 15169,
              "misses": 0,
              "total": 15169
            },
            "disk": {
              "hits": 0,
              "misses": 0,
              "total": 0
            },
            "shards": {
              "hot": {
                "hitbytes": 0,
                "missbytes": 0,
                "retrievebytes": 0
              },
              "cold": {
                "hitbytes": 0,
                "missbytes": 0,
                "retrievebytes": 0
              },
              "bypassbytes": 0
            }
          },
          "cpu": {
            "user": "00:00:01.1718750",
            "kernel": "00:00:00.0781250",
            "totalCpu": "00:00:01.2500000"
          },
          "memory": {
            "peakPerNode": 117441760
          }
        },
        "inputDatasetStatistics": {
          "extents": {
            "total": 3566,
            "scanned": 10,
            "scannedMinDatetime": "2020-09-10T19:28:59.5588356Z",
            "scannedMaxDatetime": "2020-09-10T19:28:59.5608906Z"
          },
          "rows": {
            "total": 80516126072,
            "scanned": 6433282
          },
          "rowstores": {
            "scannedRows": 2095190,
            "scannedValuesSize": 153591356
          },
          "shards": {
            "queriesGeneric": 0,
            "queriesSpecialized": 0
          }
        },
        "datasetStatistics": [
          {
            "tableRowCount": 3,
            "tableSize": 116
          }
        ]
      }
    },
    "dataSources": [
      {
        "resourceId": "/resourcegroups/ci-euwe01-siem-oms-01/providers/microsoft.operationalinsights/workspaces/",
        "region": "westeurope",
        "tables": [
          "Perf"
        ]
      }
    ]
  }
}

我在这里陷入了循环:

var clist ="";
clist = event.SearchResults.tables[0].rows;

if(event.SearchResult != '') {
  var comm ="";
  var SearchResultsw = event.SearchResults;
  var SearchResultsw = "";
  for (var i = 0; i < SearchResultsw.length; i++) {
    comm += SearchResultsw.tables[i];
    for (var j = 0; j < impactedServices[i].tables.columns.length; j++) {
      if (j != 0) {
        comm += "\n";
      }
      comm += SearchResultsw[i].tables[j].rows;
    }
    comm += "\n";
  }
  var formatfinal = "\n\ninfo:\n" + comm;
} // ?

【问题讨论】:

  • 不是很清楚你想要达到什么。请澄清。
  • 为什么要用""覆盖SearchResultsw
  • 我需要解析这些数据以获取计算机列表,但首先我需要获取 rof 列和行数,因为 thsi json 有多个级别
  • table -> columns ->rows 你是对的 var SearchResultsw = "";在 var SearchResultsw = event.SearchResults; 之前

标签: javascript arrays json parsing


【解决方案1】:

我不确定你想要什么,但这里有一个获取列数、行数和计算机信息的代码:

if(event.SearchResults != '') {
  var comm = "";
  var numColumns = 0;
  var numRows = 0;
  
  var results = event.SearchResults;
  
  //check how many columns there are
  for (var i = 0; i < results.tables[0].columns.length; i++) {
    numColumns++;
  }
  
  //check how many rows there are
  for (var j = 0; j < results.tables[0].rows.length; j++) {
    numRows++;
    
    //write the computer info(?) in the variable comm
    comm += results.tables[0].rows[j][0] + "  " + results.tables[0].rows[j][1] + "\n"
  }
  
  var formatfinal = "\n\ninfo:\n" + comm + "\ncolumns number:  " + numColumns + "\nrows number:  " + numRows;
  console.log(formatfinal)
}

这将是变量formatfinal

info:
.ci.corp  66.60977770487469
.ci.corp  60.13546142578125
.ci.corp  62.83857485453288

columns number:  2
rows number:  3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2018-04-20
    相关资源
    最近更新 更多