我还没有找到我正在寻找的确切答案,所以我想我会尝试提出这个问题。
我目前正在尝试使用 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。
然后我尝试将字符串反序列化到父类。在以下示例中,JSONStringCollection 是上面概述的 JSON 字符串,JSONStringCollection 是定义 GeneralArray 的模块:
Dim JSONString As String
JSONString = "<JSONStringDetails>"
Dim JSONObj = JsonConvert.DeserializeObject(Of JSONStringCollection.GeneralArray)(JSONString)
不幸的是,当传递这个时,返回以下内容并且例程中断:
未处理的类型异常
“Newtonsoft.Json.JsonSerializationException”发生在
Newtonsoft.Json.dll
附加信息:无法反序列化当前 JSON 数组
(例如 [1,2,3])到类型“ShadOS.JSONStringCollection+GeneralArray”中,因为
type 需要一个 JSON 对象(例如 {"name":"value"})来反序列化
正确。
我缺少什么?