gpt4 book ai didi

json - vb.NET 将 JSON 列表反序列化为对象

转载 作者:行者123 更新时间:2023-12-04 20:01:38 25 4
gpt4 key购买 nike

我还没有完全找到我正在寻找的确切答案,所以我想我会问这个问题。

我目前正在尝试使用 Json.NET 将 JSON 字符串反序列化为 vb.NET 中的对象;我之前做过一些设置适当的类,然后使用父类将字符串反序列化为一个对象,它们工作得很好,但是这个似乎并没有完全正确地分解。

我试图分解的字符串示例如下:

   [
{
"value": 12345,
"text": "Example Unique Text"
},
{
"InnerList": [
{
"value": 4746,
"text": "A duplicated entry of text"
},
{
"value": 4747,
"text": "A duplicated entry of text"
},
{
"value": 4748,
"text": "A duplicated entry of text"
},
{
"value": 4749,
"text": "A duplicated entry of text"
},
{
"value": 4750,
"text": "A duplicated entry of text"
},
{
"value": 4751,
"text": "A duplicated entry of text"
},
{
"value": 4752,
"text": "A duplicated entry of text"
},
{
"value": 4753,
"text": "A duplicated entry of text"
},
{
"value": 4754,
"text": "A duplicated entry of text"
},
{
"value": 4755,
"text": "A duplicated entry of text"
}
],
"duplicated": "Yes"
},
{
"value": 15298,
"text": "Another Example Unique Text"
},
{
"value": 959,
"text": "Yet more uniqueness"
},
{
"value": 801,
"text": "A final little bit of unique text"
}
]

我尝试通过许多外部工具传递它,它们都返回相同的类定义,但它们似乎没有工作。因此,根据我对 JSON 的理解,我整理了以下内容:
 Public Class ItemDetails

Public Value As Integer
Public Text As String

End Class

Public Class ItemArray

Public DetailList As List(Of ItemDetails)
Public Duplicated As String

End Class

Public Class GeneralArray

Public GeneralList As List(Of ItemArray)

End Class

GeneralArray 是父类,用于解析 JSON。

然后我试图将字符串反序列化为父类。在以下示例中,是上面概述的 JSON 字符串,而 JSONStringCollection 是定义 GeneralArray 的模块:
Dim JSONString As String

JSONString = "<JSONStringDetails>"

Dim JSONObj = JsonConvert.DeserializeObject(Of JSONStringCollection.GeneralArray)(JSONString)

可悲的是,当通过它时,会返回以下内容并且例程会中断:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ShadOS.JSONStringCollection+GeneralArray' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.



我错过了什么?

最佳答案

您的 JSON 字符串表示一个对象数组(您可以看到这一点,因为所有内容都包含在 [] 之间)。

错误消息说“您试图将对象数组反序列化为单个对象”。您的目标类型GeneralArray不像数组(例如,它不继承自数组/集合类型,也不实现集合接口(interface))。

另一个问题是,JSON 数组是“混合的”——它包含一些看起来像 ItemDetails 的对象。和其他看起来像 ItemArray 的对象.在像 VB.NET 这样的静态语言中,这更难以反序列化为不同类型的单个集合。

一种可能的解决方案是结合您的目标类 ItemDetailsItemArray进入一个单一的类,沿着

Public Class CombinedItem
'properties from ItemDetails
Public Value As Integer
Public Text As String

'properties from ItemArray
Public InnerList As List(Of CombinedItem)
Public Duplicated As String

End Class

鉴于此类,您的反序列化可能如下所示:
Dim JSONObj = JsonConvert.DeserializeObject(Of List(Of CombinedItem))(JSONString)

这里的关键是你告诉 Newtonsoft 目标类型是 List(Of CombinedItem) - 像目标一样的数组/集合。

现在 JSONObjCombinedItem 的集合- 有些会有 Value/ Text属性(property),其他人将拥有 InnerList/ Duplicated属性(property)。

关于json - vb.NET 将 JSON 列表反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974773/

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