使用WebServiceProxy时,ConvertFrom-JSON不会接受带子节点的convertto-json

问题描述 投票:1回答:1

我使用PowerShell 4.0中的New-WebServiceProxy从API中提取数据,然后将其传输到JSON文件以供查看并导入另一个API服务(相同的API版本等,只是一个不同的主机)。

$tasklist.Taskconfig | ConvertTo-JSON-Depth 50 -As String | Out-File -FilePath $exportpath\$name.xml -Force

给我带有TaskConfig的XML。在这种情况下,TaskConfig是由我正在连接的API自动生成的对象类型。当我想导入我正在使用的内容时:

$taskconfig      = (Get-Content "$taskjson") -join "`n" | ConvertFrom-Json

但是当我运行它时,它无法创建对象。我假设这是因为JSON包含嵌套的子节点,给出错误 -

无法转换值“@ {Name = plugindive; Value =;> Children = System.Object []}”以键入“Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig”。错误:“无法转换”@ {Name = plugindive; Value =; Children = System.Object []}“类型”System.Management.Automation.PSCustomObject“的值,键入”Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig“。”

我已经尝试明确说明对象的类型:

$taskconfig      = [Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1rcleWeb_WebClientAPI_asmx_wsdl.TaskConfig](Get-Content "$taskjson" | Out-string | ConvertFrom-Json)

以及创建对象然后尝试添加我的JSON中的子项 -

$taskconfig.children = $json.children

但这些都以同样的方式失败。

我似乎没有足够有趣地在PowerShell 5.0中得到同样的问题,但我无法验证为什么 - 还有另一种方法可以解决这个问题吗?

在下面添加了示例JSON

{"Name": "plugindive",
"Value": null,
"Children": [{
        "Name": "auto",
        "Value": "False",
        "Children": [

        ]
    },
    {
        "Name": "categories",
        "Value": null,
        "Children": [{
                "Name": "Module Z",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module A",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module B",
                "Value": "False",
                "Children": [

                ]
            },
            {
                "Name": "Module C",
                "Value": "False",
                "Children": [

                ]
            }
        ]
    }
]
}
json powershell new-webserviceproxy
1个回答
0
投票

似乎这在PowerShell v3.0中不起作用,所以我最终直接使用显式XML制作帖子,而不是转换为JSON。

© www.soinside.com 2019 - 2024. All rights reserved.