我正在連接到 REST 服務
$response = Invoke-RestMethod -Uri $URL -Headers $headers -Method POST -Body $body_json -ContentType "application/json"
$response.Outputs
我得到了那種格式的回復
Actual: {
"2017-08-29T14:37:47.137",
"2017-08-30T13:07:09.563",
"2017-08-30T14:41:29.023"
},
Start: {
"2017-08-29T14:36:12.42",
"2017-08-30T12:59:53.05",
"2017-08-30T14:40:45.34"
},
NumScrapsList: {
0,
3,
...
但我想以那種形式擁有它
{
"NumScrapsList":0,
"Actual":"2017-08-29T14:37:47.137",
"Start":"08-29T14:36:12.42"
},
{
"NumScrapsList":3,
"Actual":"2017-08-30T13:07:09.563",
"Start":"2017-08-30T12:59:53.05"
}
在 pythonic 方法中,我可以這樣做(包括“輸出”鍵):
outputs = [dict(zip(resp['Outputs'].keys(), e))
for e in zip(*resp['Outputs'].values())]
pprint(outputs)
但在 powershell 中,我不知道該怎么做。你能把我放在正確的方向嗎?
使用Invoke-RestMethod的完整$response.outputs進行編輯
$response.outputs 是
Type : {a, b, c}
Code : {xxx, yyy, eee}
CompletionDate : {1900-01-01T00:00:00, 1900-01-01T00:00:00, 1900-01-01T00:00:00}
OrderQuantity : {30, 30, 3}
NumScraps : {0, 0, 0}
ActualDate : {2021-11-16T15:17:00, 2021-11-16T15:18:00, 1900-01-01T00:00:00}
Status : {WT, FD, RT}
Order : {70000, 30794, 94098}
Sequence : {0300, 0400, 0500}
然后我可以轉換為 json 并且輸出是:
{
"Type": [
"a",
"b",
"c"
],
"Code": [
"xxx",
"yyy",
"eee"
],
"CompletionDate": [
"1900-01-01T00:00:00",
"1900-01-01T00:00:00",
"1900-01-01T00:00:00"
],
"OrderQuantity": [
30,
30,
3
],
"NumScraps": [
0,
0,
0
],
"ActualDate": [
"2021-11-16T15:17:00",
"2021-11-16T15:18:00",
"1900-01-01T00:00:00"
],
"Status": [
"WT",
"FD",
"RT"
],
"Order": [
"70000",
"30794",
"94098"
],
"Sequence": [
"0300",
"0400",
"0500"
]
}
也就是說,waitingforguacamole解決方案即使有點棘手也有效(當然,感謝您的幫助!)
uj5u.com熱心網友回復:
OK,我帶著這一個鏡頭,也許粗暴(這是既沒有功能,也不符合Python),而且我相信其他人將有遠這樣做的更多的表現方法,但是這可能是一個開始:
我清理了 JSON 看起來像這樣
{
"Actual": [
"2017-08-29T14:37:47.137",
"2017-08-30T13:07:09.563",
"2017-08-30T14:41:29.023"
],
"Start": [
"2017-08-29T14:36:12.42",
"2017-08-30T12:59:53.05",
"2017-08-30T14:40:45.34"
],
"NumScrapsList": [
0,
3,
7
]
}
(NumScrapsList為了完整起見,我添加了一個值,并將每個頂級 JSON 欄位轉換為陣列)
然后,
#simulate your REST method call result
$json = "{
`"Actual`": [
`"2017-08-29T14:37:47.137`",
`"2017-08-30T13:07:09.563`",
`"2017-08-30T14:41:29.023`"
],
`"Start`": [
`"2017-08-29T14:36:12.42`",
`"2017-08-30T12:59:53.05`",
`"2017-08-30T14:40:45.34`"
],
`"NumScrapsList`": [
0,
3,
7
]
}"
#create a field map
$fieldMap = @("NumScrapsList", "Start", "Actual")
#convert the JSON to a Powershell object, create an empty array
$in = $json | ConvertFrom-JSON
#using NumScrapsList as your iterator
0..($in.NumScrapsList.Count-1) | ForEach-Object {
$fieldMap | ForEach-Object -Begin {
#reference the outer loop index
$index = $_
#initialize an accumulator for the object whose properties to be mapped
$obj = @{}
} -Process {
#in order of fieldMap's properties, grab those fields from the input
#and add them to the accumulator by name
$obj."$_" = $in."$_"[$index]
} -End {
#return that accumulator
$obj
}
} | ConvertTo-Json
并且該處理塊可以減少為:
$fieldMap = @("NumScrapsList", "Start", "Actual");
$in = $json | ConvertFrom-JSON
0..($in.NumScrapsList.Count-1) | ForEach-Object {
$fieldMap | ForEach-Object { $index = $_; $obj = @{} } {
$obj."$_" = $in."$_"[$index]
} { $obj }
} | ConvertTo-Json
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/358612.html
