【问题标题】:Parsing a ReST response in PowerShell在 PowerShell 中解析 ReST 响应
【发布时间】:2016-03-04 06:49:09
【问题描述】:

我正在尝试在 PowerShell 中实现 ReST 请求。下面是我在 PowerShell 中的 $response。

@type            : bundleObject
id               : ZZZZZZ160000000000RU
orgId            : 000007
name             : xxxxxxxxxx
description      : Bundle being used for QA.
createTime       : 2015-04-24T15:13:24.000Z
updateTime       : 2015-04-24T15:13:24.000Z
createdBy        : xxx@gmail.com
updatedBy        : yyy@gmail.com
lastVersion      : 1.0
paid             : False
accessType       : PUBLIC
objects          : {@{@type=bundleRefObject; objectTypeCode=17; 
                   objectId=ZZZZZZ17000000000003; 
                   objectName=Mapping_01; objectDescription=; 
                   objectUpdateTime=2015-04-24T15:05:41.000Z}, 
                   @{@type=bundleRefObject; objectTypeCode=17; 
                   objectId=ZZZZZZ17000000000004; 
                   objectName=Mapping_02; objectDescription=; 
                   objectUpdateTime=2015-04-24T15:09:28.000Z}, 
                   @{@type=bundleRefObject; objectTypeCode=17; 
                   objectId=ZZZZZZ17000000000005; 
                   objectName=Mapping_03; objectDescription=; 
                   objectUpdateTime=2015-04-24T15:11:59.000Z}}
externalId       : CYIhYNVCSdC6J17N-LyA6A

在对象部分,有 3 个对象 ID。我需要将这些 ID 和名称复制到一个列表中,以备后用。我已经使用

成功获取了对象部分
$responseobject = $response.objects

但是,我不确定如何获取对象名称和对象 ID 并保存在列表中。我应该在这里使用 PSCustomObject 吗?

--------------已更新

这里还有一个查询。我使用

将值添加到哈希图中
$response_connection_hashmap = $response_connection|foreach {
    @{  $_.name = $_.id }
    }

但是,在使用键获取值时

$response_connection_hashmap.Item('Key_1')

我收到错误

Exception getting "Item": "Cannot convert argument "index", with value:  "Key_1", for "get_Item" to type "System.Int32": "Cannot convert value "Key_1" to type "System.Int32". Error: "Input string was not in a correctformat.

我还错过了什么吗?

【问题讨论】:

    标签: rest powershell powershell-5.0


    【解决方案1】:

    答案取决于您打算如何使用这些值。

    如果您只需要一个 ID 数组,则可以使用

    ($response.objects).objectIds
    

    或者,更长的形式(旧版本的 PS 是必需的)

    $response.objects | Select-Object -ExpandProperty objectIds
    

    如果需要把值一个一个拉出来操作,可以使用foreach遍历对象。

    $response.objects | foreach {
      Do-SomethingWith -id $_.objectID -updateTime $_.objectUpdateTime
    }
    

    如果您需要(用于进一步处理)一个包含所有对象的 ID 和 UpdateTime 的变量,那么您可以构建一个哈希图数组:

    $responseObjectList = $response.objects | foreach {
      @{ id = $_.objectID; updateTime = $_.objectUpdateTime }
    }
    

    或者PSCustomObjects,如果你想让它们打印得很好。

    $responseObjectList = $response.objects | foreach {
      [PSCustomObject]@{ id = $_.objectID; updateTime = $_.objectUpdateTime }
    }
    

    【讨论】:

    • 感谢您的回复!很有帮助!!我打算在进一步的 ReST 调用中使用 id 值。哈希映射通常包含一个键值对。但在我的情况下,我需要一个包含单个值列表的数组,例如 id 或 name,以便解析数组并为每个元素调用一个使用 id 作为变量的休息请求
    • 如果你想要的只是一个 ID 值的数组,它就像($response.objects).objectId 一样简单
    • 哦,好的!那很简单!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多