Azure Functions:Powershell Push-OutputBinding 格式 JSON
Azure Functions: Powershell Push-OutputBinding format JSON
我在我的 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
})
散列中键的顺序 table 未确定。
使用有序字典而不是散列table。
这将按照您的预期保留键的顺序。
为此,只需在 body
散列 table.
前面添加 [Ordered]
类型
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
})
Beginning in PowerShell 3.0, you can use the [ordered] attribute to
create an ordered dictionary
(System.Collections.Specialized.OrderedDictionary) in PowerShell.
Ordered dictionaries differ from hash tables in that the keys always
appear in the order in which you list them. The order of keys in a
hash table is not determined.
我在我的 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
})
散列中键的顺序 table 未确定。
使用有序字典而不是散列table。 这将按照您的预期保留键的顺序。
为此,只需在 body
散列 table.
[Ordered]
类型
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
})
Beginning in PowerShell 3.0, you can use the [ordered] attribute to create an ordered dictionary (System.Collections.Specialized.OrderedDictionary) in PowerShell.
Ordered dictionaries differ from hash tables in that the keys always appear in the order in which you list them. The order of keys in a hash table is not determined.