gpt4 book ai didi

powershell - 尝试将 JSON 反序列化为 Powershell 中的类型化对象

转载 作者:行者123 更新时间:2023-12-05 05:53:37 25 4
gpt4 key购买 nike

我正在尝试将 JSON 字符串(来自 API 响应)反序列化为我在 Powershell 中键入的对象,但是,我不断收到两种类型的错误。

我有两个 PowerShell 类,例如:

cpublic class DataType
{
[string] key
[string] dataName
}

public class DataTypeList
{
[string] key
[string] name
[string] localeKey
}

public class Event
{
[string] key
[string] name
[string] localeKey
[DataType] DataType
[List[DataTypeList]] DataTypeList
}

public class Root
{
[List[Event]] $events
}

我的 JSON 响应看起来像这样:

  {
"Events":[
{
"key":"1234",
"name":"Bob Muprhy",
"localeKey":"4",
"DataType":{
"key":"1111111",
"dataName":"Name One"
},
"DataTypeList":[
{
"key":"983984",
"name":"New name",
"localeKey":"34985"
},
{
"key":"124543534",
"name":"New name new Name",
"localeKey":"asdfsadf"
}
]
},
{
"key":"123456567",
"name":"Pete big",
"localeKey":"4",
"DataType":{
"key":"1111111",
"dataName":"Name 1"
},
"DataTypeList":[
{
"key":"983984",
"name":"New name",
"localeKey":"34985"
},
{
"key":"124543534",
"name":"New name new Name",
"localeKey":"asdfsadf"
}
]
}
]
}

我的代码目前看起来像这样,我正在使用 Invoke-RestMethod,因为我已经阅读过它会自动将 ConvertFrom-JSON 应用于 API 响应/内容

我在这里得到 json 响应:

$json = Invoke-RestMethod -Uri "<API Link>"

在这里,我尝试使用 Cast-Initialization 技术“将 JSON 反序列化为我的对象”

$response = [Root]($json)

但是我会收到一条错误响应:无法创建“Root”类型的对象。无法将类型“System.Object[]”的“System.Object[]”值转换为类型“System.Collections.Generic.List`1[FeaturedEvent]

我也试过做类似的事情:

$response = [Root]($json.Events)

但是,这也会返回错误无法将“System.Object[]”类型的“System.Object[]”值转换为“Root”类型

在 C# 中反序列化某些东西时,我知道我可以做类似的事情

*JsonConvert.DeserializeObject<Root>(json);*

尝试在 Powershell 中复制相同内容

我也试过这个:

 $root = [Root]::new()

$root.Events = $json.Events

但是我又收到了同样的错误

最佳答案

这是自包含的示例代码,它演示了您的方法应该 工作(将类定义中的语法错误放在一边,这些错误已在下面更正)。

  • 您问题中的示例 JSON 通过 ConvertFrom-Json 解析为 [pscustomobject] 对象图,以模拟您的 Invoke-RestMethod 调用。

  • [Root] 的简单转换足以将 [pscustomobject] 图转换为基于自定义类的强类型对象图。

  • 结果重新转换为 JSON 以证明数据已正确解析 - 请注意有关属性名称大小写的源代码注释。

using namespace System.Collections.Generic

# Your custom classes.
class DataType
{
[string] $key
[string] $dataName
}

class DataTypeList
{
[string] $key
[string] $name
[string] $localeKey
}

class Event
{
[string] $key
[string] $name
[string] $localeKey
[DataType] $DataType
[List[DataTypeList]] $DataTypeList
}

class Root
{
[List[Event]] $Events
}

# Simulate an Invoke-RestMethod call that
# returns JSON, which Invoke-RestMethod automatically
# parses into a [pscustomobject] object graph.
$fromJson = ConvertFrom-Json @'
{
"Events":[
{
"key":"1234",
"name":"Bob Muprhy",
"localeKey":"4",
"DataType":{
"key":"1111111",
"dataName":"Name One"
},
"DataTypeList":[
{
"key":"983984",
"name":"New name",
"localeKey":"34985"
},
{
"key":"124543534",
"name":"New name new Name",
"localeKey":"asdfsadf"
}
]
},
{
"key":"123456567",
"name":"Pete big",
"localeKey":"4",
"DataType":{
"key":"1111111",
"dataName":"Name 1"
},
"DataTypeList":[
{
"key":"983984",
"name":"New name",
"localeKey":"34985"
},
{
"key":"124543534",
"name":"New name new Name",
"localeKey":"asdfsadf"
}
]
}
]
}
'@

# Parse the [pscustomobject] graph into a strongly
# typed object graph based on the custom classes.
# Note: PowerShell ignores case differences between
# the property names as specified in the original JSON
# and the custom-class property names.
$fromJsonStronglyTyped = [Root] $fromJson

# Reconvert to JSON to demonstrate that roundtripping succeeds.
# Note: The property names are written with the case as defined
# in your custom classes, which may differ from the original JSON.
$fromJsonStronglyTyped | ConvertTo-Json -Depth 4

关于powershell - 尝试将 JSON 反序列化为 Powershell 中的类型化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69848181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com