Avatar of Karl Blackburn
Karl Blackburn
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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.

Any help would be appreciatted.

Kind Regards
Karl

{
   "payload":{
      "NextToken":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      "CreatedBefore":"2021-04-27T14:31:18Z",
      "Orders":[
         {
            "AmazonOrderId":"206-00000000-00000000",
            "PurchaseDate":"2021-04-20T00:03:31Z",
            "LastUpdateDate":"2021-04-20T23:20:43Z",
            "OrderStatus":"Shipped",
            "FulfillmentChannel":"MFN",
            "SalesChannel":"Amazon.co.uk",
            "ShipServiceLevel":"Std UK Dom_2",
            "OrderTotal":{
               "CurrencyCode":"GBP",
               "Amount":"16.00"
            },
            "NumberOfItemsShipped":1,
            "NumberOfItemsUnshipped":0,
            "PaymentMethod":"Other",
            "PaymentMethodDetails":[
               "Standard"
            ],
            "IsReplacementOrder":"false",
            "MarketplaceId":"A1F83G8C2ARO7P",
            "ShipmentServiceLevelCategory":"Standard",
            "OrderType":"StandardOrder",
            "EarliestShipDate":"2021-04-20T23:00:00Z",
            "LatestShipDate":"2021-04-22T22:59:59Z",
            "EarliestDeliveryDate":"2021-04-23T23:00:00Z",
            "LatestDeliveryDate":"2021-04-28T22:59:59Z",
            "IsBusinessOrder":false,
            "IsSoldByAB":false,
            "IsPrime":false,
            "IsGlobalExpressEnabled":false,
            "IsPremiumOrder":false,
            "DefaultShipFromLocationAddress":{
               "Name":"null",
               "AddressLine1":"null",
               "AddressLine2":"null",
               "City":"Aberdeen",
               "PostalCode":"AB12 0DN",
               "CountryCode":"GB",
               "Phone":"null"
            },
            "IsISPU":false
         },
         {
            "AmazonOrderId":"206-00000000-00000000",
            "PurchaseDate":"2021-04-20T05:55:06Z",
            "LastUpdateDate":"2021-04-20T23:20:30Z",
            "OrderStatus":"Shipped",
            "FulfillmentChannel":"MFN",
            "SalesChannel":"Amazon.co.uk",
            "ShipServiceLevel":"Std UK Dom_2",
            "OrderTotal":{
               "CurrencyCode":"GBP",
               "Amount":"16.00"
            },
            "NumberOfItemsShipped":1,
            "NumberOfItemsUnshipped":0,
            "PaymentMethod":"Other",
            "PaymentMethodDetails":[
               "Standard"
            ],
            "IsReplacementOrder":"false",
            "MarketplaceId":"A1F83G8C2ARO7P",
            "ShipmentServiceLevelCategory":"Standard",
            "OrderType":"StandardOrder",
            "EarliestShipDate":"2021-04-20T23:00:00Z",
            "LatestShipDate":"2021-04-22T22:59:59Z",
            "EarliestDeliveryDate":"2021-04-23T23:00:00Z",
            "LatestDeliveryDate":"2021-04-28T22:59:59Z",
            "IsBusinessOrder":false,
            "IsSoldByAB":false,
            "IsPrime":false,
            "IsGlobalExpressEnabled":false,
            "IsPremiumOrder":false,
            "DefaultShipFromLocationAddress":{
               "Name":"null",
               "AddressLine1":"null",
               "AddressLine2":"null",
               "City":"Aberdeen",
               "PostalCode":"AB12 0DN",
               "CountryCode":"GB",
               "Phone":"null"
            },
            "IsISPU":false
         }
      ]
   }
}

Open in new window


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

Open in new window


ASP.NETJSONVisual Basic.NET

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
Fabrice Lambert

Karl Blackburn

ASKER
Hi,

That appears to be a desktop app, I need the above to work in asp.net / vb on a webserver.

Kind Regards
Karl
Fabrice Lambert

These are library that you'll need to integrate within your application.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.