[英]Unable to deserialize json file
下面是我的 json 文本
[{
"Type": "Type Value",
"Name": "Name Value",
"Elements": {"Element1": {"one":"1", "two":"2"}, "Element2": {"three":"3", "four":"4"}}
}]
以下是我的 class
Public Class TestJson
Public Property Property1 As Class2()
End Class
Public Class Class2
Public Property Type As String
Public Property Name As String
Public Property Elements As Elements
End Class
Public Class Elements
Public Property Element1 As Element1
Public Property Element2 As Element2
End Class
Public Class Element1
Public Property one As String
Public Property two As String
End Class
Public Class Element2
Public Property three As String
Public Property four As String
End Class
下面使用 newtownsoft 反序列化拋出異常
Dim classObj = JsonConvert.DeserializeObject(Of TestJson)(JsonText)
Below is the exception Cannot deserialize the current JSON array (eg [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' because the type requires a JSON object (例如 {"name":"value"}) 以正確反序列化。 要修復此錯誤,請將 JSON 更改為 JSON object(例如 {"name":"value"})或將實現的反序列化類型接口更改為數組或數組從 JSON 陣列反序列化。 JsonArrayAttribute 也可以添加到類型中以強制它從 JSON 數組反序列化。 路徑 '',第 1 行,position 1。
根據錯誤消息,您將 TestJson 的集合反序列化為單個實例。 像這樣更新你的反序列化調用,看看它是如何進行的
Dim classObj = JsonConvert.DeserializeObject(Of List(Of TestJson))(testJson)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.