Results 1 to 5 of 5

Thread: Getting Data from a Json array

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Northampton, UK
    Posts
    59

    Getting Data from a Json array

    Hi Everyone,

    I hope someone can explain something for me

    I have the following JSON data that is sent from my supplier


    Code:
    {
      "balances": {
        "currency": "GBP",
        "issuerTotals": {},
        "totalAmount": 0,
        "totalRefundsAmount": 0,
        "totalRefundsCount": 0,
        "totalSalesAmount": 0,
        "totalSalesCount": 0,
        "totalCashbackAmount": 0,
        "totalCashbackCount": 0,
        "totalGratuityAmount": 0,
        "totalGratuityCount": 0,
        "totalsSince": "15/07/21 17:10",
        "waiterTotals": {}
      },
      "banking": {
        "Cardnet MS": {
          "currency": "",
          "currentSessionIssuerTotals": {},
          "previousSessionIssuerTotals": {},
          "currentSessionTotals": {
            "currency": "",
            "totalAmount": 0,
            "totalRefundsAmount": 0,
            "totalRefundsCount": 0,
            "totalSalesAmount": 0,
            "totalSalesCount": 0
          },
          "previousSessionTotals": {
            "currency": "",
            "totalAmount": 0,
            "totalRefundsAmount": 0,
            "totalRefundsCount": 0,
            "totalSalesAmount": 0,
            "totalSalesCount": 0
          },
          "currentSessionTransactionNumbers": [
            "",
            ""
          ],
          "previousSessionTransactionNumbers": [
            "",
            ""
          ]
        }
      },
      "reportLines": [
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "END OF DAY"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "Z BALANCES"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "Totals reset"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "Thank you"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "15/07/21 17:17"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "HANDSET:01"
        },
        {
          "format": [],
          "type": "LINE_SEPARATOR_DOUBLE",
          "value": "========"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "GRAND TOTALS"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "Since 15/07/21 17:10"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_HEIGHT"
          ],
          "type": "TEXT",
          "value": "No Business"
        },
        {
          "format": [],
          "type": "LINE_SEPARATOR_DOUBLE",
          "value": "========"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "REPORT COMPLETE"
        },
        {
          "format": [],
          "type": "TICKET_FEED",
          "value": "\n\n\n\n\n"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "END OF DAY"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "BANKING     "
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "   "
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_HEIGHT"
          ],
          "type": "TEXT",
          "value": "Algpos                  "
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_HEIGHT"
          ],
          "type": "TEXT",
          "value": "NN4 6EP"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "Thank you"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "TID:22165264"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "15/07/21 17:17"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "HANDSET:01"
        },
        {
          "format": [],
          "type": "LINE_SEPARATOR_DOUBLE",
          "value": "========"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "Cardnet MS"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "TOTALS CONFIRMED"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "15/07/21 17:17"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "CURRENT SESSION:       0"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_HEIGHT"
          ],
          "type": "TEXT",
          "value": "No Business"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "PREVIOUS SESSION:"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "No Business"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "TXN 0094"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "COMPLETED"
        },
        {
          "format": [],
          "type": "TEXT",
          "value": "DIAG 3576"
        },
        {
          "format": [],
          "type": "LINE_SEPARATOR_DOUBLE",
          "value": "========"
        },
        {
          "format": [
            "BOLD",
            "DOUBLE_WIDTH"
          ],
          "type": "TEXT",
          "value": "REPORT COMPLETE"
        }
      ],
      "reportTime": "2021-07-15T17:17:00",
      "reportType": "END_OF_DAY",
      "reportResult": "COMPLETED",
      "notifications": [
        "REPORT_FINISHED",
        "REPORT_STARTED"
      ],
      "userMessage": ""
    }
    What I would like explained please is how do I get the data from the sub Functions. (when I mean sub functions i mean for example

    Code:
    "notifications": [
        "REPORT_FINISHED",
        "REPORT_STARTED"
      ]
    or everything that is under the "reportLines" header

    I would apreachate any advice anyone can give me

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting Data from a Json array

    What have you tried and where exactly are you stuck? There's loads of information around the web about working with JSON but you seem not to have actually attempted to use any of it. If you haven't tried anything, how do you know that you can't do it? If you have tried something, show us what it is and tell us exactly where and how it didn't behave as you expected.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2014
    Location
    Northampton, UK
    Posts
    59

    Re: Getting Data from a Json array

    I can work with JSON data no problem but I will admit I have never had to get data from inside a JSON node

    for example

    "reportLines": [
    {
    "format": [
    "BOLD",
    "DOUBLE_WIDTH"
    ],
    "type": "TEXT",
    "value": "END OF DAY"
    },
    {
    "format": [
    "BOLD",
    "DOUBLE_WIDTH"
    ],

    I am not sure how to get the information


    I can get to this part

    Code:
    Dim json As String = responseFromServer
    Dim ser As JObject = JObject.Parse(json)
    Dim data As List(Of JToken) = ser.Children().ToList
    For Each item As JProperty In data
    item.CreateReader()
    Debug.Print(item.Name, item.Value)
    Select Case item.Name
    Case "notifications"
    ' do somthing with the Data in the notifications feild
    End Select
    Next
    But i am not sure how to move forward

    as you can see the notifications hold the following data

    "notifications": [
    "REPORT_FINISHED",
    "REPORT_STARTED"
    ]

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Getting Data from a Json array

    If you have the complete schema o json object(that from what you write, you possibly have) you can convert it to vb classes through either Visual Studio (I don't have VS here but there is somewhere a JSON-convert to object or something) or by a free tool. Then you just feed the data with Newtonsoft.Json .
    Last edited by sapator; Jul 17th, 2021 at 06:27 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Getting Data from a Json array

    Visual Studio has a cool feature called Paste JSON as Classes that can be found under Edit > Paste Special > Paste JSON as Classes. If you were to do this, then you would get something that looks like the following:
    Code:
    Public Class Rootobject
        Public Property balances As Balances
        Public Property banking As Banking
        Public Property reportLines() As Reportline
        Public Property reportTime As Date
        Public Property reportType As String
        Public Property reportResult As String
        Public Property notifications() As String
        Public Property userMessage As String
    End Class
    
    Public Class Balances
        Public Property currency As String
        Public Property issuerTotals As Issuertotals
        Public Property totalAmount As Integer
        Public Property totalRefundsAmount As Integer
        Public Property totalRefundsCount As Integer
        Public Property totalSalesAmount As Integer
        Public Property totalSalesCount As Integer
        Public Property totalCashbackAmount As Integer
        Public Property totalCashbackCount As Integer
        Public Property totalGratuityAmount As Integer
        Public Property totalGratuityCount As Integer
        Public Property totalsSince As String
        Public Property waiterTotals As Waitertotals
    End Class
    
    Public Class Issuertotals
    End Class
    
    Public Class Waitertotals
    End Class
    
    Public Class Banking
        Public Property CardnetMS As CardnetMS
    End Class
    
    Public Class CardnetMS
        Public Property currency As String
        Public Property currentSessionIssuerTotals As Currentsessionissuertotals
        Public Property previousSessionIssuerTotals As Previoussessionissuertotals
        Public Property currentSessionTotals As Currentsessiontotals
        Public Property previousSessionTotals As Previoussessiontotals
        Public Property currentSessionTransactionNumbers() As String
        Public Property previousSessionTransactionNumbers() As String
    End Class
    
    Public Class Currentsessionissuertotals
    End Class
    
    Public Class Previoussessionissuertotals
    End Class
    
    Public Class Currentsessiontotals
        Public Property currency As String
        Public Property totalAmount As Integer
        Public Property totalRefundsAmount As Integer
        Public Property totalRefundsCount As Integer
        Public Property totalSalesAmount As Integer
        Public Property totalSalesCount As Integer
    End Class
    
    Public Class Previoussessiontotals
        Public Property currency As String
        Public Property totalAmount As Integer
        Public Property totalRefundsAmount As Integer
        Public Property totalRefundsCount As Integer
        Public Property totalSalesAmount As Integer
        Public Property totalSalesCount As Integer
    End Class
    
    Public Class Reportline
        Public Property format() As String
        Public Property type As String
        Public Property value As String
    End Class
    From here, you can deserialize the JSON response to the Rootobject class which in turn you have access to strongly typed objects to get your values:
    Code:
    Dim json As String = responseFromServer
    Dim jsonObject = JsonConvert.DeserializeObject(Of Rootobject)(json)
    Console.WriteLine(String.Join(", ", jsonObject.notifications))
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width