I am trying to parse a Json String that is returned let is say from a service. For simplicity it will be like:
Dim jsonstring = _
<s>
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
</s>.Value
Now I am using the following function to read the json string to an object however all the properties are always nothing. I have created an object for each json object in the above string and the object has Datacontract and Datamember attributes. Any tips please?
Dim menu = JsonObject(Of Menu)(jsonstring)
Console.WriteLine(menu.value)
Private Function JsonObject(Of t)(jsonString As String) As t
Dim ser As New DataContractJsonSerializer(GetType(t))
Dim ms As New MemoryStream(Encoding.UTF8.GetBytes(jsonString))
ms.Position = 0
Dim obj As t = DirectCast(ser.ReadObject(ms), t)
Return obj
End Function
By looking at this call:
Console.WriteLine(menu.value)
I am suspecting that the Menu class is not defined correctly... Does your Menu class has a property called 'menu' and does the type of this 'menu' property is a complex type with the properties called 'id', 'value', 'popup'?