Link to home
Start Free TrialLog in
Avatar of Karl Blackburn
Karl BlackburnFlag 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


Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Avatar of 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
These are library that you'll need to integrate within your application.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial