我在我的 azure PowerShell HTTP 函式中使用了以下代碼。觸發此功能時,我收到 JSON 正文,但與我在代碼中提供的順序不同。請讓我知道如何以與我編碼相同的順序接收 JSON 正文作為回應,即{"Status": "val", "Msg": "val", "Value": "val"}
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Headers = @{
"Content-type" = "application/json"
}
Body = @{
"Status" = $status
"Msg" = $body
"Value" = $val
} | ConvertTo-Json
})
uj5u.com熱心網友回復:
哈希表中鍵的順序是不確定的。
使用有序字典而不是哈希表。這將按照您的預期保留密鑰的順序。
為此,只需在哈希表[Ordered]前面添加型別。body
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Headers = @{
"Content-type" = "application/json"
}
# Will keep the order of the keys as specified.
Body = [Ordered]@{
"Status" = $status
"Msg" = $body
"Value" = $val
} | ConvertTo-Json
})
從 PowerShell 3.0 開始,您可以使用 [ordered] 屬性在 PowerShell 中創建有序字典 (System.Collections.Specialized.OrderedDictionary)。
有序字典與哈希表的不同之處在于,鍵總是按照您列出它們的順序出現。哈希表中鍵的順序是不確定的。
資源
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/443922.html
標籤:json 电源外壳 天蓝色函数 无服务器 天蓝色的PowerShell
