反序列化在vb.net JSON数组 [英] Deserialize json array in vb.net
问题描述
我有被格式化JSON数组如下:
I have a json array which is formatted as follows:
[
{
"property":96,
"listofstuff":[
{
"anotherproperty":"some text here",
"yetanother":"text goes here too"
}
],
"lastproperty":3001
},
<rest of array>
]
我如何反序列化这一点,这样我可以有通过索引对象列表的方式财产
?含义,我希望能够获得这样的数据: MYLIST(96).lastproperty
或 MYLIST(96).listofstuff.yetanother
,并将它返回正确的数据类型呢?是,甚至有可能在vb.net?
How can I deserialize this in such a way that I can have a list of objects indexed by property
? Meaning, I want to be able to access the data like this: MyList(96).lastproperty
or MyList(96).listofstuff.yetanother
and have it return the proper datatype too? Is that even possible in vb.net?
推荐答案
我同意你需要使用JSON库Json.NET在的 HTTP://json.$c$cplex.com/
I agree that the JSON library you need to use is Json.NET found at http://json.codeplex.com/
因此,考虑到你的榜样JSON数组,我做了如下类,可用于序列化和反序列化:
So, given your example JSON array, I made the following classes that can be used for serializing and deserializing:
Public Class Item
Public Property [property]() As Integer
Public Property listofstuff() As Stuff()
Public Property lastproperty() As Integer
End Class
Public Class Stuff
Public Property anotherproperty() As String
Public Property yetanother() As String
End Class
然后,所有你需要的是下面的code到能够访问大致你想要的方式中的数据:
Then all you need is the following code to be able to access the data in roughly the way you wanted to:
Dim Items = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Item())(json)
Dim MyList = Items.ToDictionary(Function(x) x.property)
Dim Stuff = MyList(96).listofstuff(0)
如果用 listofstuff
属性数组你的意图是要存储任何字符串一双然后用这个定义项目
(你也不会需要的东西
类):
If your intent with the listofstuff
property array was to store any string pair then use this definition for Item
(and you also won't need the Stuff
class):
Public Class Item
Public Property [property]() As Integer
Public Property listofstuff() As Dictionary(Of String, String)()
Public Property lastproperty() As Integer
End Class
这篇关于反序列化在vb.net JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!