vb.NET将JSON列表反序列化为对象 [英] vb.NET Deserialise JSON list into object
问题描述
我还无法完全找到所需的答案,以至于我不敢问这个问题.
I've not quite been able to find the exact answer for which I'm looking so thought I'd have a crack at asking the question.
我目前正在尝试使用Json.NET将JSON字符串反序列化为vb.NET中的对象;通过设置适当的类,然后使用父类将字符串反序列化为一个对象,我已经做了一些工作,但是它们工作得很好,但是似乎并不能很好地解决这个问题.
I'm currently attempting to deserialise a JSON string into an object in vb.NET using Json.NET; I've done a few before by setting up appropriate Classes and then deserialising the string to an object using the Parent Class and they've worked fine however this one just doesn't seem to break down quite right.
我要分解的字符串示例如下:
An example of the string I'm trying to break down is as follows:
[
{
"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"
}
]
我尝试通过许多外部工具进行传递,它们都返回了相同的Class定义,但是它们似乎没有用.因此,基于对JSON的理解,我将以下内容组合在一起:
I've tried passing this through a number of external tools and they all come back with the same Class definitions however they haven't seemed to work. So based on my understanding of JSON I put together the following:
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的东西.
GeneralArray is the Parent Class and is what's being used to parse the JSON.
然后我尝试将字符串反序列化为父类.在下面的示例中,是上面概述的JSON字符串,而JSONStringCollection是在其中定义GeneralArray的模块:
I'm then trying to deserialise the string to the Parent Class. In the following example, is the JSON String outlined above and JSONStringCollection is the Module in which GeneralArray is defined:
Dim JSONString As String
JSONString = "<JSONStringDetails>"
Dim JSONObj = JsonConvert.DeserializeObject(Of JSONStringCollection.GeneralArray)(JSONString)
可悲的是,当通过此操作时,将返回以下内容,并且例程中断:
Sadly, when passing this through, the following is returned and the routine breaks:
类型的未处理异常 "Newtonsoft.Json.JsonSerializationException"发生在 Newtonsoft.Json.dll
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
其他信息:无法反序列化当前JSON数组 (例如[1,2,3])转换为'ShadOS.JSONStringCollection + GeneralArray'类型,因为 类型需要JSON对象(例如{"name":"value"})进行反序列化 正确.
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字符串表示对象的数组(您可以看到此信息,因为所有内容都包裹在[
和]
之间)
Your JSON string represents an array of objects (you can see this because everything is wrapped between [
and ]
).
错误消息说:您试图将对象数组反序列化为单个对象".您的目标类型GeneralArray
的行为不像数组(例如,它不继承自数组/集合类型,也不实现集合接口).
The error message is saying "you tried to deserialize an array of objects into a single object". Your target type GeneralArray
does not behave like an array (e.g. it does not inherit from an array/collection type nor does it implement a collection interface).
另一个问题是,JSON数组是混合"的-它包含一些看起来像ItemDetails
的对象和其他看起来像ItemArray
的对象.在像VB.NET这样的静态语言中,更难将其反序列化为不同类型的单个集合.
The other issue is, the JSON array is "mixed" - it contains some objects that look like ItemDetails
and other objects that look like ItemArray
. In a static language like VB.NET, this is more difficult to deserialize into a single collection of distinct types.
一种可能的解决方案是将您的目标类ItemDetails
和ItemArray
组合为一个类,
One possible solution is to combine your target classes ItemDetails
and ItemArray
into a single class, along the lines of
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
鉴于此类,您的反序列化可能类似于:
Given this class, your deserialization might look like:
Dim JSONObj = JsonConvert.DeserializeObject(Of List(Of CombinedItem))(JSONString)
关键是要告诉Newtonsoft目标类型是List(Of CombinedItem)
-类似于目标的数组/集合.
The key here is you telling Newtonsoft that the target type is a List(Of CombinedItem)
- an array/collection like target.
现在JSONObj
是CombinedItem
的集合-有些将具有Value
/Text
属性,另一些将具有InnerList
/Duplicated
属性.
Now JSONObj
is a collection of the CombinedItem
- some will have a Value
/Text
property, others will have a InnerList
/Duplicated
property.
这篇关于vb.NET将JSON列表反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!