2

我有一个 json 数组,其格式如下:

[
  {
    "property":96,
    "listofstuff":[
      {
        "anotherproperty":"some text here",
        "yetanother":"text goes here too"
      }
    ],
    "lastproperty":3001
  },
 <rest of array>
]

我怎样才能以这样的方式反序列化它,以便我可以拥有一个由 索引的对象列表property?意思是,我希望能够像这样访问数据:MyList(96).lastproperty或者MyList(96).listofstuff.yetanother让它也返回正确的数据类型?这在 vb.net 中甚至可能吗?

4

2 回答 2

2

我同意您需要使用的 JSON 库是位于http://json.codeplex.com/的 Json.NET

因此,给定您的示例 JSON 数组,我创建了以下可用于序列化和反序列化的类:

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

那么您所需要的只是以下代码,以便能够以您想要的大致方式访问数据:

Dim Items = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Item())(json)
Dim MyList = Items.ToDictionary(Function(x) x.property)
Dim Stuff = MyList(96).listofstuff(0)

如果您对属性数组的意图listofstuff是存储任何字符串对,则将此定义用于Item(并且您也不需要Stuff该类):

Public Class Item
    Public Property [property]() As Integer
    Public Property listofstuff() As Dictionary(Of String, String)()
    Public Property lastproperty() As Integer
End Class
于 2011-02-01T23:44:05.563 回答
-1

您需要一个用于 .net 的 JSON 库:http: //json.codeplex.com/

于 2011-02-01T22:53:29.157 回答