Deserialize JSON in ASP.net / vb and Keep getting a Null Value
Hi,
I am trying to Deserialize some JSON data received via an API in the below format, everytime I try to run the code, which I believe to be correct the line - For Each order In payload.Orders - falls over with a Null error.
Public Class OrderTotal <JsonProperty("CurrencyCode")> Public Property CurrencyCode As String <JsonProperty("Amount")> Public Property Amount As String End Class Public Class DefaultShipFromLocationAddress <JsonProperty("Name")> Public Property Name As String <JsonProperty("AddressLine1")> Public Property AddressLine1 As String <JsonProperty("AddressLine2")> Public Property AddressLine2 As String <JsonProperty("City")> Public Property City As String <JsonProperty("PostalCode")> Public Property PostalCode As String <JsonProperty("CountryCode")> Public Property CountryCode As String <JsonProperty("Phone")> Public Property Phone As String End Class Public Class Order <JsonProperty("AmazonOrderId")> Public Property AmazonOrderId As String <JsonProperty("PurchaseDate")> Public Property PurchaseDate As DateTime <JsonProperty("LastUpdateDate")> Public Property LastUpdateDate As DateTime <JsonProperty("OrderStatus")> Public Property OrderStatus As String <JsonProperty("FulfillmentChannel")> Public Property FulfillmentChannel As String <JsonProperty("SalesChannel")> Public Property SalesChannel As String <JsonProperty("ShipServiceLevel")> Public Property ShipServiceLevel As String <JsonProperty("OrderTotal")> Public Property OrderTotal As OrderTotal <JsonProperty("NumberOfItemsShipped")> Public Property NumberOfItemsShipped As Integer <JsonProperty("NumberOfItemsUnshipped")> Public Property NumberOfItemsUnshipped As Integer <JsonProperty("PaymentMethod")> Public Property PaymentMethod As String <JsonProperty("PaymentMethodDetails")> Public Property PaymentMethodDetails As List(Of String) <JsonProperty("IsReplacementOrder")> Public Property IsReplacementOrder As String <JsonProperty("MarketplaceId")> Public Property MarketplaceId As String <JsonProperty("ShipmentServiceLevelCategory")> Public Property ShipmentServiceLevelCategory As String <JsonProperty("OrderType")> Public Property OrderType As String <JsonProperty("EarliestShipDate")> Public Property EarliestShipDate As DateTime <JsonProperty("LatestShipDate")> Public Property LatestShipDate As DateTime <JsonProperty("EarliestDeliveryDate")> Public Property EarliestDeliveryDate As DateTime <JsonProperty("LatestDeliveryDate")> Public Property LatestDeliveryDate As DateTime <JsonProperty("IsBusinessOrder")> Public Property IsBusinessOrder As Boolean <JsonProperty("IsSoldByAB")> Public Property IsSoldByAB As Boolean <JsonProperty("IsPrime")> Public Property IsPrime As Boolean <JsonProperty("IsGlobalExpressEnabled")> Public Property IsGlobalExpressEnabled As Boolean <JsonProperty("IsPremiumOrder")> Public Property IsPremiumOrder As Boolean <JsonProperty("DefaultShipFromLocationAddress")> Public Property DefaultShipFromLocationAddress As DefaultShipFromLocationAddress <JsonProperty("IsISPU")> Public Property IsISPU As Boolean End Class Public Class Payload <JsonProperty("NextToken")> Public Property NextToken As String <JsonProperty("CreatedBefore")> Public Property CreatedBefore As DateTime <JsonProperty("Orders")> Public Property Orders As List(Of Order) 'Public Property Orders As Dictionary(Of String, Order) End Class Public Class Root <JsonProperty("payload")> Public Property Payload As Payload End Class Public Shared Function OrderImport() As String Try Dim result = restClient.Execute(request) If result.StatusCode = System.Net.HttpStatusCode.OK Then Dim payload = JsonConvert.DeserializeObject(Of Payload)(result.Content) For Each order In payload.Orders 'Console.WriteLine(order.Key) 'Dim AmazonOrderId As String = Order.AmazonOrderId 'MsgBox(AmazonOrderId.ToString) 'Dim orderDate As DateTime = order.OrderDetails.OrderDate 'For Each item In Order.OrderDetails.Items 'Dim vendorSku As String = item.VendorProductIdentifier 'Dim price As String = item.NetPrice.Amount 'Dim qty As Integer = item.OrderedQuantity.Amount 'Next Next ElseIf result.StatusCode = System.Net.HttpStatusCode.Forbidden Then Return result.Content Else Return result.Content End If Catch e As Exception Throw End Try End Function
json - VBJSON for VB6 how to serialize object returned from Parse routine - Stack Overflow