VS2017環境のVB.NETで、以下のようなjsonをパースしようとしましたが、エラーで困っています。
以下のようなjsonなのですが、contentのmenusが配列構造になっており、このgradeとmarkの値を取得する方法がわかりません。
//////////// json ///////////////
{
"response": {
"id": "xxxxxx",
"content": {
"menus": [
{
"grade": "A",
"mark": "1"
},
{
"grade": "B",
"mark": "2"
},
{
"grade": "C",
"mark": "3"
},
{
"grade": "D",
"mark": "4"
}
]
}
}
}
/////////// jsonここまで ///////////
VS標準の以下を参照してデシリアライズしています。
[参照]
Imports System.Runtime.Serialization.Json
[デシリアライズクラス]
Public Function Deserialize(Of T)(ByVal json As String) As T
Dim result As T
Dim serializer As New DataContractJsonSerializer(GetType(T))
Using stream As New IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)) result = DirectCast(serializer.ReadObject(stream), T) End Using Return result
End Function
Public Class grade_select_Rootobject
Public Property response As Response
End Class
Public Class grade_select_Response
Public Property id As String
Public Property content As grade_select_Content
End Class
Public Class grade_select_Content
Public Property menus() As grade_select_Menu
End Class
Public Class grade_select_Menu
Public Property grade As String
Public Property mark As String
End Class
以下は実行部分とそのエラーです。
[jsonの読み込み]
Rootobject = Deserialize(Of grade_select_Rootobject)(json)
[menusの件数取得]
Dim count As Integer = Rootobject.response.content.menus.Count
→ 9が取得できます。
この後、取得した件数(9)でループをしながら、各gradeとmarkを取得できると考えていました。
Dim i As Integer
For i = 0 To count - 1
MsgBox(grade_select_Rootobject.response.content.menus(i).grade.ToString)
Next
ループ内のメッセージボックスで、gradeの値が1件ずつ表示されると想定していましたが、
エラーメッセージは、「公開メンバー'grade'は型'Object'にみつかりませんでした」です。
どなたかお知恵のある方、解決策あるいは小職がミスをしている箇所をご指摘いただけないでしょうか。
よろしくお願いいたします。

回答1件
あなたの回答
tips
プレビュー