从Json迭代一个deserealized .Net Object来创建一个DataTable

时间:2017-10-16 22:54:57

标签: json vb.net object service

我花了很多时间考虑如何从具有许多项目,子项目和子子项目的deserealized json迭代对象。我知道已经发布了很多问题,但我仍然没有得到它,因为这些例子与我的不同,我是一个初学者。我从Web服务中获取Json,并通过标题和所有内容传递令牌,但我不知道操纵此信息的最佳方法。我正在使用Newtonsoft库,BTW。

Json是:

{
  "orders": [
    {
      "customer": {
        "id": "string",
        "externalId": "string",
        "firstName": "string",
        "lastName": "string",
        "name": "string",
        "companyName": "string",
        "emailAddresses": [
          "string"
        ],
        "phones": [
          {
            "type": "Mobile",
            "number": "string"
          }
        ],
        "addresses": [
          {
            "lines": [
              "string"
            ],
            "locality": [
              "string"
            ],
            "region": "string",
            "postcode": "string"
          }
        ]
      },
      "vehicles": [
        {
          "id": "string",
          "description": "string",
          "vin": "string",
          "odometer": 0,
          "tag": "string",
          "nextServiceDue": "2015-07-14T10:08:15Z",
          "registrationTestDue": "2015-07-14T10:08:15Z",
          "brand": "string",
          "extendedWarrantyEndDate": 0,
          "contractEndDate": 0,
          "transmissionCode": "string",
          "engineCode": "string",
          "bodyType": "string",
          "paintCode": "string",
          "modelCode": "string",
          "references": [
            {
              "key": "string",
              "value": "string"
            }
          ]
        }
      ],
      "employees": [
        {
          "id": "string",
          "name": "string",
          "type": "Technician"
        }
      ],
      "operations": [
        {
          "id": "35955",
          "code": "RWB",
          "externalCode": "RACS",
          "description": "431151: Front Shock Absorber Assembly (One Side), Repair/Replace",
          "isNew": true,
          "labour": {
            "pricing": {
              "totals": {
                "total": 0
              }
            },
            "times": [
              {
                "quantity": 1.2
              }
            ]
          }
        }
      ],
      "parts": [
        {
          "number": "string",
          "quantity": 1,
          "quantityUnitOfMeasure": "Hour",
          "description": "string",
          "vehicleId": "string",
          "pricing": {
            "totals": {
              "total": 0
            }
          }
        }
      ]
    }
  ],
  "paging": {
    "page": 0,
    "morePages": true,
    "resultCount": 0,
    "pageCount": 0
  }
}

获取json属性的代码是:

Public Class Rootobject
    Public Property orders() As Order
    Public Property paging As Paging
End Class

Public Class Paging
    Public Property page As Integer
    Public Property morePages As Boolean
    Public Property resultCount As Integer
    Public Property pageCount As Integer
End Class

Public Class Order
    Public Property customer As Customer
    Public Property vehicles() As Vehicle
    Public Property employees() As Employee
    Public Property operations() As Operation
    Public Property parts() As Part
End Class

Public Class Customer
    Public Property id As String
    Public Property externalId As String
    Public Property firstName As String
    Public Property lastName As String
    Public Property name As String
    Public Property companyName As String
    Public Property emailAddresses() As String
    Public Property phones() As Phone
    Public Property addresses() As Address
End Class

Public Class Phone
    Public Property type As String
    Public Property number As String
End Class

Public Class Address
    Public Property lines() As String
    Public Property locality() As String
    Public Property region As String
    Public Property postcode As String
End Class

Public Class Vehicle
    Public Property id As String
    Public Property description As String
    Public Property vin As String
    Public Property odometer As Integer
    Public Property tag As String
    Public Property nextServiceDue As Date
    Public Property registrationTestDue As Date
    Public Property brand As String
    Public Property extendedWarrantyEndDate As Integer
    Public Property contractEndDate As Integer
    Public Property transmissionCode As String
    Public Property engineCode As String
    Public Property bodyType As String
    Public Property paintCode As String
    Public Property modelCode As String
    Public Property references() As Reference
End Class

Public Class Reference
    Public Property key As String
    Public Property value As String
End Class

Public Class Employee
    Public Property id As String
    Public Property name As String
    Public Property type As String
End Class

Public Class Operation
    Public Property id As String
    Public Property code As String
    Public Property externalCode As String
    Public Property description As String
    Public Property isNew As Boolean
    Public Property labour As Labour
End Class

Public Class Labour
    Public Property pricing As Pricing
    Public Property times() As Time
End Class

Public Class Pricing
    Public Property totals As Totals
End Class

Public Class Totals
    Public Property total As Integer
End Class

Public Class Time
    Public Property quantity As Single
End Class

Public Class Part
    Public Property number As String
    Public Property quantity As Integer
    Public Property quantityUnitOfMeasure As String
    Public Property description As String
    Public Property vehicleId As String
    Public Property pricing As Pricing1
End Class

Public Class Pricing1
    Public Property totals As Totals1
End Class

Public Class Totals1
    Public Property total As Integer
End Class

我实现了Json,并且我已经将此作为一个例子:

            Dim JsonObject As List(Of Rootobject) = JsonConvert.DeserializeObject(Of List(Of Rootobject))(_JsonString)

        For Each item In JsonObject
            _PagPage = item.paging.page
            _PagMorePag = item.paging.morePages
            _PagResCount = item.paging.resultCount
            _PagPagCount = item.paging.pageCount
        Next

我尝试使用List(Of Rootobject)List(Of List(Of Dictionary(Of Rootoobject))这样的列表,但它仍无效。

如果你能帮助我,我真的很感激。

0 个答案:

没有答案