vb.NET 将 JSON 列表反序列化为对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28974773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.NET Deserialise JSON list into object
提问by Your_Unequal
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.
我一直无法找到我正在寻找的确切答案,所以我想我可以提出这个问题。
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.
我目前正在尝试使用 Json.NET 将 JSON 字符串反序列化为 vb.NET 中的对象;我之前做过一些设置适当的类,然后使用父类将字符串反序列化为一个对象,它们运行良好,但是这个似乎并没有完全正确地分解。
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"
}
]
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:
我已经尝试通过许多外部工具传递它,并且它们都返回相同的类定义,但是它们似乎没有工作。因此,基于我对 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 is the Parent Class and is what's being used to parse the JSON.
GeneralArray 是父类,用于解析 JSON。
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:
然后我试图将字符串反序列化到父类。在以下示例中,是上面概述的 JSON 字符串,JSONStringCollection 是定义 GeneralArray 的模块:
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:
可悲的是,当通过它时,返回以下内容并且例程中断:
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.
Newtonsoft.Json.dll 中发生类型为“Newtonsoft.Json.JsonSerializationException”的未处理异常
附加信息:无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ShadOS.JSONStringCollection+GeneralArray”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化.
What am I missing with this?
我错过了什么?
采纳答案by kaveman
Your JSON string represents an arrayof objects (you can see this because everything is wrapped between [and ]).
您的 JSON 字符串表示一组对象(您可以看到这一点,因为所有内容都包含在[和之间])。
The error message is saying "you tried to deserialize an array of objects into a single object". Your target type GeneralArraydoes not behave like an array (e.g. it does not inherit from an array/collection type nor does it implement a collection interface).
错误消息是“您试图将一组对象反序列化为单个对象”。您的目标类型的GeneralArray行为不像数组(例如,它不从数组/集合类型继承,也不实现集合接口)。
The other issue is, the JSON array is "mixed" - it contains some objects that look like ItemDetailsand 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.
另一个问题是,JSON 数组是“混合的”——它包含一些ItemDetails看起来像ItemArray. 在像 VB.NET 这样的静态语言中,这更难以反序列化为不同类型的单个集合。
One possible solution is to combine your target classes ItemDetailsand ItemArrayinto a single class, along the lines of
一个可能的解决方案是你的目标类相结合ItemDetails,并ItemArray成一个类,沿行
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)
The key here is you telling Newtonsoft that the target type is a List(Of CombinedItem)- an array/collection like target.
这里的关键是你告诉 Newtonsoft 目标类型是一个List(Of CombinedItem)- 一个类似目标的数组/集合。
Now JSONObjis a collection of the CombinedItem- some will have a Value/Textproperty, others will have a InnerList/Duplicatedproperty.
现在JSONObj是一个集合CombinedItem- 有些会有Value/Text属性,有些会有InnerList/Duplicated属性。

