使用 vb.net 反序列化多级 JSON 字符串

Deserialize multilevel JSON string with vb.net

我一直在尝试反序列化 vb.net 中的 json 字符串。我能够使用

成功获得响应
            Using myResp = TryCast(myReq.GetResponse(), System.Net.HttpWebResponse)
                Using myReader = New System.IO.StreamReader(myResp.GetResponseStream())
                    responseContent = myReader.ReadToEnd()

                End Using
            End Using

响应内容:

{
    "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."
            }]
        }]

已创建 类 以保存数据:

    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

当我尝试反序列化时,没有抛出任何错误,但没有填充任何内容:

VB debug

请让我知道我可能做错了什么?

至于我的想法,

首先,看起来Json的形式是错误的。 缺少最后一点 }}。

第二,对象不对。 属性 名称必须与 Json 名称相同。 如果 属性 名称不同,则应使用 Json属性.

进行标记

我应用了内容并做了一个测试源。

 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