【问题标题】:How to load a JSON file and convert it to an object of a specific type?如何加载 JSON 文件并将其转换为特定类型的对象?
【发布时间】:2016-06-22 03:30:33
【问题描述】:

我有一个 FooObject 类型,我有一个从 FooObject 实例序列化的 JSON 文件。现在我想使用 ConvertFrom-Json 将 JSON 文件加载到内存并将命令的输出转换为 FooObject 对象,然后在只接受 FooObject 作为参数的 cmdlet Set-Bar 中使用新对象输入。

但我注意到ConvertFrom-Json 的输出类型是PSCustomObject,我没有找到任何方法将PSCustomObject 转换为FooObject

【问题讨论】:

    标签: json powershell deserialization powershell-3.0


    【解决方案1】:

    尝试将自定义对象转换为FooObject

    $foo = [FooObject](Get-Content 'C:\path\to\your.json' | Out-String | ConvertFrom-Json)
    

    如果这不起作用,请尝试使用输入对象的属性构造 FooObject 实例(前提是该类具有类似的构造函数):

    $json = Get-Content 'C:\path\to\your.json' | Out-String | ConvertFrom-Json
    $foo = New-Object FooObject ($json.Foo, $json.Bar, $json.Baz)
    

    如果这也不起作用,您需要创建一个空的 FooObject 实例并在之后更新其属性:

    $json = Get-Content 'C:\path\to\your.json' | Out-String | ConvertFrom-Json
    $foo = New-Object FooObject
    $foo.AA = $json.Foo
    $foo.BB = $json.Bar
    $foo.CC = $json.Baz
    

    【讨论】:

    • 因为我拥有 Set-Bar 的代码(在 C# 中)。我是否可以让它接受PSCustomObject 并在内部将其转换为FooObject?对于我的 cmdlet 用户来说,哪种方式更好?
    • 如果您将接口更改为接受PSCustomObjectObject,应该可以。不过,我对 C# 不太熟悉。
    • 你应该使用Get-Content -Raw 'C:\path\to\your.json' 来加载你的json内容
    【解决方案2】:

    从这里:https://blogs.technet.microsoft.com/heyscriptingguy/2014/04/23/powertip-convert-json-file-to-powershell-object/

    我发现以下作品很棒:

    Get-Content -Raw -Path <jsonFile>.json | ConvertFrom-Json
    

    【讨论】:

    • 更短:Get-Content &lt;jsonFile&gt; | ConvertFrom-Json
    • 如果未指定类型,如何将 JSON 转换为 特定 类型? ?
    【解决方案3】:

    我意识到这是一篇旧帖子,但我发现了一种更有效的方法,如果投射它不起作用。一定要先试一下。只要您的类不包含自定义类型的嵌套集合,强制转换就会起作用。假设您的班级如下所示。

    class Container 
    {
        [string] $Id
        [string] $Name
        [System.Collections.Generic.List[Process]] $Processes
    }
    class Process
    {
        [string] $Id
        [string] $Name
    }
    

    ConvertFrom-Json 会将其转换为 [PSCustomObject],但会将 List[Process] 转换为 Object[],这会导致任何强制转换操作引发以下异常。

    无法将“System.Object[]”类型的“System.Object[]”值转换为“System.Collections.Generic.List`1[Process]”类型。

    ConvertToFinalInvalidCastException

    使用以下方法来反序列化这种类型的层次结构。

    $serializer = [System.Web.Script.Serialization.JavaScriptSerializer]::new()
    
    $content = $serializer.Deserialize((Get-Content -Path $JsonFilePath), [YourCustomType])
    

    [System.Web.Script.Serialization.JavaScriptSerializer]ConvertFrom-Json 在后台工作的方式。因此,我刚刚创建了一个新实例,并且能够将多级(确切地说是四个级别,每个级别都有一个低于它的级别的集合)json文件转换为我的powershell类容易地。我也意识到这可以简化为以下内容,但上面更容易阅读。

    $content = [System.Web.Script.Serialization.JavaScriptSerializer]::new().Deserialize((Get-Content -Path $JsonFilePath), [YourCustomType])
    

    【讨论】:

    • 在尝试使用这种方法时,我遇到了: > 无法从程序集 'System.Web,Version=4.0.0.0,Culture=neutral 加载类型 'System.Web.UI.WebResourceAttribute' , PublicKeyToken=b03f5f7f11d50a3a'。这是在同时添加了Add-Type -AssemblyName System.WebAdd-Type -AssemblyName System.Web.Extensions
    • 我不确定@KristianWilliams。我从来不需要将类型添加到我的会话中。如果您可以使用ConvertFrom-Json,它应该已经可用。就像我在回答中所说的那样,这是 cmdlet 使用的程序集。因此,您不需要添加类型。
    猜你喜欢
    • 2021-07-21
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    相关资源
    最近更新 更多