ConvertFrom-json中的哈希表与Powershell内置哈希表的类型不同,如何使它们相同? [英] Hashtables from ConvertFrom-json have different type from powershells built-in hashtables, how do I make them the same?

查看:119
本文介绍了ConvertFrom-json中的哈希表与Powershell内置哈希表的类型不同,如何使它们相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的json文件(test.json):

I have a json file (test.json) that looks something like this:

{
    "root":
    {
        "key":"value"
    }
}

我正在使用类似这样的东西将其加载到powershell中:

I'm loading it into powershell using something like this:

PS > $data = [System.String]::Join("", [System.IO.File]::ReadAllLines("test.json")) | ConvertFrom-Json

root
----
@{key=value}

我希望能够枚举由json文件定义的"hashtable"之类的对象的键.因此,理想情况下,我希望能够做点什么像:

I'd like to be able to enumerate the keys of the 'hashtable' like object that is defined by the json file. So, Ideally, I'd like to be able to do something like:

$data.root.Keys

并返回["key"].我可以使用powershell中的内置哈希表执行此操作,但是使用从json加载的哈希表执行此操作不太明显.

and get ["key"] back. I can do this with the built-in hashtables in powershell, but doing this with a hashtable loaded from json is less obvious.

在对此进行故障排除时,我注意到ConvertFrom-json返回的字段具有与Powershell哈希表不同的类型.例如,在内置哈希表上调用.GetType()会使它显示为'Hashtable'类型:

In troubleshooting this, I've noticed that the fields returned by ConvertFrom-json have different types than those of Powershell's hashtables. For example, calling .GetType() on a built-in hashtable makes shows that it's of type 'Hashtable':

PS > $h = @{"a"=1;"b"=2;"c"=3}
PS > $h.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object

对我的json对象执行相同操作会产生PSCustomObject:

doing the same for my json object yields PSCustomObject:

PS > $data.root.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    PSCustomObject                           System.Object

是否有任何方法可以将该对象转换或转换为典型的Powershell哈希表?

Is there any way to cast or transform this object into a typical powershell Hashtable?

推荐答案

ConvertFrom-Json cmdlet为您提供了一个自定义对象,因此您必须使用点表示法而不是作为下标来访问它们.通常,您会知道期望在JSON中使用哪些字段,因此,一般而言,这比获取哈希表更有用.建议您不要使用它,而不是通过转换回哈希表来对抗系统.

The ConvertFrom-Json cmdlet gives you a custom object so you have to access them using dot notation rather than as a subscript. Usually you would know what fields you expect in the JSON so this is actually more useful in general than getting a hash table. Rather than fighting the system by converting back to a hash table, I suggest you work with it.

您可以将select与通配符属性名称一起使用以获取属性:

You can use select with wildcard property names to get at the properties:

PS D:\> $data = @"
{
    "root":
    {
        "key":"value", "key2":"value2", "another":42
    }
}
"@ | ConvertFrom-Json

PS D:\> $data.root | select * | ft -AutoSize

key   key2   another
---   ----   -------
value value2      42



PS D:\> $data.root | select k* | ft -AutoSize

key   key2  
---   ----  
value value2

Get-Member如果要提取可迭代的属性名称列表:

and Get-Member if you want to extract a list of property names that you can iterate over:

PS D:\> ($data.root | Get-Member -MemberType NoteProperty).Name
another
key
key2

将其放入循环中将得到如下代码:

Putting that into a loop gives code like this:

PS D:\> foreach ($k in ($data.root | Get-Member k* -MemberType NoteProperty).Name) {
    Write-Output "$k = $($data.root.$k)"
    }
key = value
key2 = value2

这篇关于ConvertFrom-json中的哈希表与Powershell内置哈希表的类型不同,如何使它们相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆