2

次のようにフォーマットされたjson配列があります。

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

オブジェクトのリストにインデックスを付けることができるように、これを逆シリアル化するにはどうすればよいpropertyですか?つまり、次のようにデータにアクセスできるようにしたいのですが、それとも適切なデータ型を返すようにしますかMyList(96).lastpropertyMyList(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 に答える