I have been trying to deserialize json string in vb.net. I am able successfully get a response using
Using myResp = TryCast(myReq.GetResponse(), System.Net.HttpWebResponse)
Using myReader = New System.IO.StreamReader(myResp.GetResponseStream())
responseContent = myReader.ReadToEnd()
End Using
End Using
responseContent:
{
"devices": {
"totalCount": 1,
"totalPage": 1,
"pageNum": 0,
"transactions": [{
"transactionId": "20211005200111",
"state": "Complete",
"type": "Put",
"operationType": "UPLOAD",
"devices": [{
"imei": "357452100344123",
"code": 40000004,
"message": "DEVICE_INVALID",
"data": "The specified device is not valid."
}, {
"imei": "357452100344409",
"code": 40000005,
"message": "DEVICE_DUPLICATE",
"data": "The specified device already exists."
}]
}]
created classes to hold data:
Public Class devices
Public Property devicelist As List(Of device)
End Class
Public Class device
Public Property pageNum As Integer
Public Property totalCount As Integer
Public Property totalPage As Integer
Public Property transactions As List(Of transaction)
End Class
Public Class transaction
Public Property transactionId As String
Public Property state As String
Public Property type As String
Public Property operationType As String
Public Property devices As List(Of mydevice)
End Class
Public Class mydevice
Public Property imei As String
Public Property code As Integer
Public Property message As String
Public Property data As String
End Class
When I attempt to deserialize, no error is thrown however nothing gets populated:
Please let me know what I may be doing wrong?
CodePudding user response:
As for my thoughts,
First of all, it looks like the Json form is wrong. The last point }} is missing.
Second, Object is wrong. The property name must be the same as the Json name. If property name is different, it should be marked using JsonProperty.
I applied the contents and made a test source.
Public Class root
<JsonProperty("devices")>
Public Property devicelist As devices
End Class
Public Class devices
Public Property pageNum As Integer
Public Property totalCount As Integer
Public Property totalPage As Integer
Public Property transactions As List(Of transaction)
End Class
Public Class transaction
Public Property transactionId As String
Public Property state As String
Public Property type As String
Public Property operationType As String
Public Property devices As List(Of mydevice)
End Class
Public Class mydevice
Public Property imei As String
Public Property code As Integer
Public Property message As String
Public Property data As String
End Class
Private Sub test()
Try
Dim Json As String = "{
'devices': {
'totalCount': 1,
'totalPage': 1,
'pageNum': 0,
'transactions': [{
'transactionId': '20211005200111',
'state': 'Complete',
'type': 'Put',
'operationType': 'UPLOAD',
'devices': [{
'imei': '57452100344123',
'code': 40000004,
'message': 'DEVICE_INVALID',
'data': 'The specified device is not valid.'
}, {
'imei': '357452100344409',
'code': 40000005,
'message': 'DEVICE_DUPLICATE',
'data': 'The specified device already exists.'
}]
}]
}}"
Dim ds As root = JsonConvert.DeserializeObject(Of root)(Json)
Dim d As devices = ds.devicelist
Console.WriteLine(d.pageNum)
Console.WriteLine(d.totalCount)
Console.WriteLine(d.totalPage)
For Each tran In d.transactions
Console.WriteLine(tran.transactionId)
For Each dd In tran.devices
Console.WriteLine(dd.code)
Next
Next
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub