我正在嘗試將 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]”型別
I tried also to do something like:
$response = [Root]($json.Events)
However, that will also return error Cannot convert the "System.Object[]" value of type "System.Object[]" to type "Root
When deserializing something in C# i know I would be able to do something like
*JsonConvert.DeserializeObject<Root>(json);*
Trying to replica the same here in Powershell
I also did try this:
$root = [Root]::new()
$root.Events = $json.Events
However again I received the same error
uj5u.com熱心網友回復:
下面是自包含的示例代碼,它演示了您的方法應該有效(將類定義中的語法錯誤放在一邊,下面已更正)。
您問題中的示例 JSON 通過 決議為
[pscustomobject]物件圖ConvertFrom-Json,以模擬您的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
uj5u.com熱心網友回復:
您的資料型別有錯誤。公共類 DataTypeList 應僅根據 $json 具有 [DataType[]]$DataTypeList。這應該將您的 json 反序列化為自定義 [Root] 型別:
class DataType {
[string]$key;
[string]$dataName;
}
class DataTypeList {
[DataType[]]$DataTypeList;
}
class Event {
[string]$key;
[string]$name;
[string]$localeKey;
[DataType]$DataType;
[Collections.Generic.List[DataTypeList]]$DataTypeList;
}
class Root {
[Collections.Generic.List[Event]]$events;
}
$content = ([System.Web.Script.Serialization.JavaScriptSerializer]::new()).Deserialize($json, [Root])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/351185.html
標籤:powershell
