我还没有完全找到我所寻找的确切答案,所以想试着问一下这个问题。
我目前正尝试使用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是定义GeneralArray的模块,jsonString是上面概述的JSON字符串:
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”,因为该类型需要一个 JSON 对象(例如 {"name":"value"})才能正确反序列化。