【问题标题】:Adding a timestamp in a JSON body在 JSON 正文中添加时间戳
【发布时间】:2019-10-11 09:27:15
【问题描述】:

我对 powershell 还很陌生,还不习惯。我正在尝试实时远程控制我的一些服务器。 为此,我使用 RestAPI 向我发送 JSON 格式的数据(Powershell 会自动转换为 PSCustomObject)。

问题是我的 JSON/数据 API 中没有字段时间戳,所以我不得不添加它。不幸的是,powershell 中的 JSON 工具似乎有限,我无法直接添加它。我必须创建一个名为 timestamp 的新 PSCustomObject 并将其与来自 API 的数据结合起来。

不幸的是,数据没有被正确解析。 我遍历一个字符串,一个字符串数组,一个哈希表。这是最糟糕的。 最后我下载了一个我现在正在尝试使用的 JSON.NET 框架......

所以这里是主要的代码部分:

Function Combine-Objects { 
    Param (
        [Parameter(mandatory=$true)]$Object1, 
        [Parameter(mandatory=$true)]$Object2
    )

    $arguments = [Pscustomobject]@() 
    foreach ( $Property in $Object1.psobject.Properties){
        $arguments += @{$Property.Name = $Property.value}  
    }

    foreach ( $Property in $Object2.psobject.Properties){
        $arguments += @{ $Property.Name= $Property.value}         
    }

    $Object3 = [Pscustomobject]$arguments

    return $Object3
    }

while (1) 
{
    $startpoint = "REST API URL"
    $endpoint = "POWER BI API URL"
    $response = Invoke-RestMethod -Method Get -Uri $startpoint 
    $timestamp=get-date -format yyyy-MM-ddTHH:mm:ss.fffZ 
    $response2 = [PsCustomObject]@{"timestamp"=$timestamp}
    $response3 = Combine-Objects -Object1 $response -Object2 $response2
    Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($response3))
}

在组合 PSCustomObject 之前,我有:

    total     : 8589463552
    available : 5146566656
    percent   : 40,1
    used      : 3442896896
    free      : 5146566656

转换后它给了我一个正确的 JSON

    [
        {
            "total":  8589463552,
            "available":  5146566656,
            "percent":  40.1,
            "used":  3442896896,
            "free":  5146566656
        }
    ]

在合并并添加时间戳后,我有:

    Name                           Value                                                                                                                                                           
    ----                           -----                                                                                                                                                           
    total                          8589463552                                                                                                                                                      
    available                      5146566656                                                                                                                                                      
    percent                        40,1                                                                                                                                                            
    used                           3442896896                                                                                                                                                      
    free                           5146566656                                                                                                                                                      
    timestamp                      2019-10-09T22:17:18.734Z    

转换后给我的:

    [
        {
            "total":  8589463552
        },
        {
            "available":  5146566656
        },
        {
            "percent":  40.1
        },
        {
            "used":  3442896896
        },
        {
            "free":  5146566656
        },
        {
            "timestamp":  "2019-10-09T22:17:18.734Z"
        }
    ]

虽然我想要的只是:

    [
     {
      "total" :98.6,
      "available" :98.6,
      "percent" :98.6,
      "used" :98.6,
      "free" :98.6,
      "timestamp" :"2019-10-11T09:21:04.981Z"
     }
    ]

【问题讨论】:

  • $arguments = [Pscustomobject]@() -> $arguments = @{} 在你的 Combine-Objects 函数中

标签: json rest powershell api pscustomobject


【解决方案1】:

我认为你看错了。为什么不简单地将包含DateTime stringNoteProperty 添加到PSCustomObject,然后将其转换回json 格式?

这样就足够了:

$json = @"
 [
    {
         "total":  8589463552,
         "available":  5146566656,
         "percent":  40.1,
         "used":  3442896896,
         "free":  5146566656
     }
  ]
"@
$jsonConverted = $json | ConvertFrom-Json
    
$params = @{
   NotePropertyName  = 'timestamp'
   NotePropertyValue = (Get-Date).ToString('yyyy-MM-ddTHH:mm:ss.fff')
}
$jsonConverted | Add-Member @params
    
$newJson = $jsonConverted | ConvertTo-Json  
$newJson

这将产生您正在寻找的json 对象:

{
    "total":  8589463552,
    "available":  5146566656,
    "percent":  40.1,
    "used":  3442896896,
    "free":  5146566656,
    "timestamp":  "2019-10-11T13:33:05.673"
}

【讨论】:

  • 嗨,Darklite1,非常感谢!我确实在考虑 add-member 函数,但我对类型感到困惑。所以我相信 JSON 默认被 Powershell 视为 PSCustomObject 。尽管我仍然有问题:{ "total": 8589463552, ... "timestamp": "2019-10-11T13:33:05.673" } 而不是 [ { "total" :98.6, ... "timestamp" :"2019-10-11T09:21:04.981Z" }]
  • 成功了,谢谢!我刚刚添加了类似$output = "['n " + $outputTEMP + "'n]" 的内容,以获得正确的格式
  • Yw :) 要实现json 对象的array 格式,您需要使用$NewJson = ConvertTo-Json @($JsonConverted) 而不是$NewJson = $JsonConverted | ConvertTo-Json。找到答案here
  • 完美,绝对比我的解决方法更干净。不错!
  • 符号[ .. ]json 格式中表示它是array,表示对象的集合或一组对象。 PowerShell 中的 array 表示为 @( .. )。很高兴我能对你有所帮助。继续探索,玩得越多,你就会得到更好的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2013-06-06
  • 2011-11-21
  • 2014-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多