Home > database >  VB.NET Deserialize JSON with Newton
VB.NET Deserialize JSON with Newton

Time:07-27

I'm trying to get to the next level of information from a jSon response. I can get "code", but I need to get the "result" and some other information that's under "payload".

I don't have a ton of experience with this, so any help would be appreciated!

Here's my code

        Dim jason As String = str
        Dim parsejson As New JObject
        parsejson = JObject.Parse(jason)
        Dim theResponseYo As String = parsejson.SelectToken("code").ToString()
        MsgBox("You are: "   theResponseYo)

Here's the json I'm parsing:

{
  "code": "200",
  "status": "success",
  "exchange-id": "xxxxxxx",
  "payload": {
    "transaction": {
      "amount": "2.30",
      "id": "115340330",
      "created": "2022-07-26 20:07:21.157",
      "type": "SALE",
      "result": "APPROVED",
      "card": "XXXXXXXXXXXX1111",
      "csc": "999",
      "authorization-code": "TAS185",
      "batch-string-id": "10",
      "display-message": "Transaction approved",
      "result-code": "000",
      "exp-date": "1222",
      "card-type": "VISA",
      "sales-tax-amount": "3.00",
      "last-four": "1111",
      "service-fee": "1.00",
      "merchant-id": "12345567",
      "terminal-id": "10011111",
      "level3": {
        "vat-tax-amount": "0.00",
        "vat-tax-rate": "0.00",
        "order-date": "20220726",
        "discount-amount": "0.00",
        "destination-country-code": "USA",
        "freight-amount": "0.00",
        "duty-amount": "0.00",
        "level3-items": {
          "level3-items": [
            {
              "item-description": "PRODUCT",
              "item-commodity-code": "UPC",
              "item-quantity": "1.00",
              "item-unit-of-measure": "EA",
              "item-product-code": "MATERIAL",
              "item-tax-rate": "0.00",
              "item-discount": "0.00",
              "item-discount-rate": "0.00"
            }
          ]
        }
      }
    },
    "payloadType": "transaction"
  }
}

CodePudding user response:

There are 2 ways - you can create VB.Net classes and deserialize instead of parsing if you need the most data, or if you need only several values you can do it like this

 Dim code As String = parsejson("code").ToString()

 Dim code As String = parsejson("payload")("transaction")("result").ToString()

if you want to deserialize

Dim data as Data = JsonConvert.DeserializeObject(Of Data)(jason)

classes

 Public Class Data
        Public Property code As String
        Public Property status As String
        Public Property exchange-id As String
        Public Property payload As Payload
    End Class

Public Class Level3Items
        Public Property item-description As String
        Public Property item-commodity-code As String
        Public Property item-quantity As String
        Public Property item-unit-of-measure As String
        Public Property item-product-code As String
        Public Property item-tax-rate As String
        Public Property item-discount As String
        Public Property item-discount-rate As String
    End Class

    Public Class Level3Items
        Public Property level3-items As Level3Items()
    End Class

    Public Class Level3
        Public Property vat-tax-amount As String
        Public Property vat-tax-rate As String
        Public Property order-date As String
        Public Property discount-amount As String
        Public Property destination-country-code As String
        Public Property freight-amount As String
        Public Property duty-amount As String
        Public Property level3-items As Level3Items
    End Class

    Public Class Transaction
        Public Property amount As String
        Public Property id As String
        Public Property created As String
        Public Property type As String
        Public Property result As String
        Public Property card As String
        Public Property csc As String
        Public Property authorization-code As String
        Public Property batch-string-id As String
        Public Property display-message As String
        Public Property result-code As String
        Public Property exp-date As String
        Public Property card-type As String
        Public Property sales-tax-amount As String
        Public Property last-four As String
        Public Property service-fee As String
        Public Property merchant-id As String
        Public Property terminal-id As String
        Public Property level3 As Level3
    End Class

    Public Class Payload
        Public Property transaction As Transaction
        Public Property payloadType As String
    End Class
  • Related