简体   繁体   English

我想从odata网址提取JSON文件中存在的数据,但是我无法使用循环提取内容

[英]I want to extract the data present in JSON file from a odata url but i am unable to extract the content using a loop

Below is the JSON file which i am referring.. 以下是我要引用的JSON文件。

{
  "@odata.context": "https://ac.com/odata/$metadata#ModelVariableDataTypes(Id,ModelVariable,DoubleValues,ModelVariable(Id,Name,UnitOfMeasure,UnitOfMeasure(Name,Abbreviation)),DoubleValues(Id,Value,Timestamp,DataQuality,DataQuality(Id,Name)))",
  "@odata.count": 1,
  "value": [
    {
      "Id": 1928155,
      "ModelVariable": {
        "Id": 1929663,
        "Name": "AccCore_CPULoadProcess",
        "UnitOfMeasure": {
          "Name": "%",
          "Abbreviation": "%"
        }
      },
      "DoubleValues": [
        {
          "Id": 75865549,
          "Value": 0.0,
          "Timestamp": "2018-09-25T03:35:00Z",
          "DataQuality": {
            "Id": 1,
            "Name": "Good"
          }
        },
        {
          "Id": 75865729,
          "Value": 0.0,
          "Timestamp": "2018-09-25T03:40:00Z",
          "DataQuality": {
            "Id": 1,
            "Name": "Good"
          }
        },
        {
          "Id": 75865873,
          "Value": 0.0,
          "Timestamp": "2018-09-25T03:45:00Z",
          "DataQuality": {
            "Id": 1,
            "Name": "Good"
          }
        }
      ]
    }
  ]
}

I want to extract the data present in JSON file from a odata url but i am unable to extract the content using a loop as in the JSON file the odata count is mentioned as 1(@odata.count": 1) so when i am trying to catch the entire data using a loop it is not working. 我想从odata URL中提取JSON文件中存在的数据,但是我无法像JSON文件中那样使用循环来提取内容,因此odata计数被提到为1(@ odata.count“:1),所以当我试图使用循环捕获整个数据,但它不起作用。

I want to extract the data present in the array field of doublevalues and wanted to show the output of the top three values of CPU process. 我想提取双值数组字段中存在的数据,并想显示CPU进程的前三个值的输出。

I am trying with the below code to extract the JSON data. 我正在尝试使用下面的代码来提取JSON数据。

$path= "C:\Users\s.papolu\Desktop\mem.json"
$data = Get-Content -Path 'C:\Users\S.Papolu\Desktop\mem.json' | ConvertFrom-Json 

$maxCount = $data.'@odata.count'
$maxCount

@"
for ($i = 0; $i -lt $maxCount; $i++)
{
$Name = $("{0:N1}" -f $data.value.ModelVariable.Name)
$cpu = $("{$i`:N2}" -f $data.value.DoubleValues.Value)
}

write-host $Name,$cpu

I'm not quite sure what you're asking here. 我不太确定您在这里问什么。 For one thing, you've got an extraneous at-sign and double-quote in there. 一方面,您在其中有多余的符号和双引号。

MaxCount is just 1 here, so the loop only runs once. MaxCount在这里仅为1,因此循环仅运行一次。 For each item, though, are you trying to store the results in some structure and then show it? 但是,对于每个项目,您是否尝试将结果存储在某种结构中然后显示出来? Which of the DoubleValues do you want? 您想要哪个DoubleValues? The top three? 前三名?

Is this closer to what you want? 更接近您想要的吗?

$data = Get-Content -Path $path | ConvertFrom-Json

$maxCount = $data.'@odata.count'
$maxCount

$results = @()

for ($i = 0; $i -lt $maxCount; $i++)
{
    # get the top three doubles values
    $Doubles = @($data.value[$i].DoubleValues | sort -Property Value -Descending | select -first 3)

    $results += [PsCustomObject]@{
        Name = $data.value[$i].ModelVariable[0].Name;
        Cpu = $Doubles;
    }
}

$results | ft

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

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