【问题标题】:Using Powershell Dictionary使用 Powershell 字典
【发布时间】:2023-01-27 00:25:26
【问题描述】:

我在使用非常基本的 Powershell 脚本时遇到了困难。

我有一个对象“ids”(我不能确切地说出它是什么类型)像这样转换为 json

[{"id": "3ccbfe7a-e381-ed11-81ad-000d3aba6139","RowKey": "204640","PartitionKey": "ppm"},
 {"id": "7339255d-e381-ed11-81ad-000d3aba6139","RowKey": "205269","PartitionKey": "ppm"}]

我只是想通过“rowkey”获得“ids”。例如

    $ids["204640"] shall resolve to "3ccbfe7a-e381-ed11-81ad-000d3aba6139".

很明显,这应该很容易——但我失败了……我的理解是,它是一个具有命名属性的对象列表或数组。我假设一些简单的施法魔法是必要的......

(我的调试技能非常有限,因为我是 Powershell 新手,我在 azure 门户中编写脚本,尝试使用表绑定编写 Azure 函数。实际上“ids”来自该绑定。)

【问题讨论】:

  • 假设您已经使用 ConvertFrom-Json 作为 Json 字符串,那么您可以简单地使用 $json.id

标签: powershell


【解决方案1】:

目前还不清楚您的 Json 字符串是否已经转换为对象,因为您可以使用 ConvertFrom-Json。然后你可以在RowKey属性上使用Group-Object -AsHashtable分组:

$string = '
[
  {"id": "3ccbfe7a-e381-ed11-81ad-000d3aba6139","RowKey": "204640","PartitionKey": "ppm"},
  {"id": "7339255d-e381-ed11-81ad-000d3aba6139","RowKey": "205269","PartitionKey": "ppm"}
]
'

$json = (ConvertFrom-Json $string) | Group-Object RowKey -AsHashTable
$json['205269'].id # => 7339255d-e381-ed11-81ad-000d3aba6139

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2018-06-21
    • 2020-05-02
    • 2018-07-18
    相关资源
    最近更新 更多