【问题标题】:beginner question regarding multidimensional array关于多维数组的初学者问题
【发布时间】:2020-05-18 01:32:46
【问题描述】:

所以我从我的 Azure 订阅中收集到了这些从 json 转换而来的信息:

$Request += (((az consumption usage list --subscription $subscriptionID --start-date $CycleStart --end-date $CycleEnd) 2> $null | ConvertFrom-Json) | select subscriptionname, instanceName, usageStart, usageEnd, pretaxCost, tags )

这会产生一个看起来不错的数组:

$Request 

subscriptionName : Sub1
instanceName : VM1
usageStart : 2020-01-27T00:00:00Z
usageEnd : 2020-01-27T23:59:59Z
pretaxCost : 3.194
tags : @{Classification=ABC, Zone=123, Field=qaz, Org=CAL, Owner=Bob@dev}

subscriptionName : Sub1
instanceName : VM2
usageStart : 2020-01-27T00:00:00Z
usageEnd : 2020-01-27T23:59:59Z
pretaxCost : 2.1
tags : @{Classification=zzz, Zone=123, Field=qaz, Org=NZ, Owner=John@prod}

但正如您所见,“标签”字段看起来在 @{xxxx} 之间合并。经过数小时的反复试验(!),我想要实现并且无法实现的目标是这样的:

subscriptionName : Sub1
instanceName : VM1
usageStart : 2020-01-27T00:00:00Z
usageEnd : 2020-01-27T23:59:59Z
pretaxCost : 3.194
Classification : ABC
Zone : 123
Field : qaz
Org : CAL
Owner : Bob@dev

subscriptionName : Sub1
instanceName : VM2
usageStart : 2020-01-27T00:00:00Z
usageEnd : 2020-01-27T23:59:59Z
pretaxCost : 2.1
Classification : ABC
Zone : 222
Field : BIZ
Org : NZ
Owner : John@dev

但是...嵌套在“标签”下的字段可能会随着时间而改变,因此我无法硬编码会查找“分类”、“区域”、“字段”等的内容...)

谁能帮我解决这个问题? :)

我偶然发现了那个脚本,认为它可以提供帮助,但无法让它发挥作用.. https://github.com/solidstate888/JSON-ToCSV

【问题讨论】:

  • ConvertFrom-Json 有一个-Depth parameter
  • 是的,但默认情况下深度为 1024,因此应该可以工作.. 但我认为 ConvertFrom-Json 无法正确解析字段“标签”,因为这看起来不像格式正确的 json ...
  • @frank 查看我的工作代码答案

标签: arrays json azure powershell nested


【解决方案1】:

tags 字段的外观来看,我相信它是一个哈希表。

您可以通过检查其中一个数组元素中的 tags 属性的类型来验证这一点:

$Request[0].tags | Get-Member

应该显示:

因此,对于每个哈希表条目,您所要做的就是输出每个哈希表键(即字段名称)及其哈希表值(即字段值)

请注意,注释#code to output the input array 上方的代码块仅用于创建一个两行数组来模拟我在您的问题中看到的内容,我认为您需要的代码(输出数组内容的代码) 位于评论 #code to output the input array 下方。特别是,请参阅循环 foreach($tag in $object.tags.keys)。此循环输出输入数组的tags 字段(哈希表)中存在的任何键+值对集合。这涉及您问题中的陈述:

但是...嵌套在“标签”下的字段会随着时间而改变

请注意,我的模拟对象在标签哈希表中有两组不同的键+值对,我的代码处理输出不同的内容。

cls

#code to create simulated array that looks like the content of your question
#In your program, I assume that this array is created by using ConvertFrom-Json 
$Request = New-Object System.Collections.ArrayList
$field = [ordered]@{}

$field.subscriptionName  = 'Sub1'
$field.instanceName = 'VM1'
$field.usageStart  = Get-Date -Date '2020-01-27T00:00:00Z'
$field.usageEnd = Get-Date -Date '2020-01-27T23:59:59Z'
$field.pretaxCost = 3.194
$field.tags = @{Classification='ABC'; Zone=123; Field='qaz'; Org='CAL'; Owner='Bob@dev'}
$Request.Add((New-Object PSObject -Property $field)) | out-null

$field.subscriptionName  = 'Sub1'
$field.instanceName = 'VM2'
$field.usageStart  = Get-Date -Date '2020-01-27T00:00:00Z'
$field.usageEnd = Get-Date -Date '2020-01-27T23:59:59Z'
$field.pretaxCost = 2.1
#Note that this tags hash table has content that is different when compared to the tags hash table in the first element of the array above
$field.tags = @{Foo1='foo1'; Foo2="Foo2"; Field='qaz'; Foo3=6677}
$Request.Add((New-Object PSObject -Property $field)) | out-null

#code to output the input array

foreach ($fieldItem in $Request)
{
    write-host ("subscriptionName={0}" -f $fieldItem.subscriptionName)
    write-host ("instanceName={0}" -f $fieldItem.instanceName)
    write-host ("usageStart={0}" -f $fieldItem.usageStart)
    write-host ("usageEnd={0}" -f $fieldItem.usageEnd)
    write-host ("pretaxCost={0}" -f $fieldItem.pretaxCost)

    foreach($tag in $fieldItem.tags.keys)
    {
        write-host ("{0}={1}" -f $tag, $fieldItem.tags.$tag)
    }

    write-host
}   

输出

【讨论】:

    猜你喜欢
    • 2020-05-10
    • 2011-01-12
    • 2018-01-08
    • 2013-05-02
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多