|
-
Jul 16th, 2021, 08:25 PM
#1
Thread Starter
Member
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
-
Jul 16th, 2021, 10:08 PM
#2
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.
-
Jul 17th, 2021, 01:20 PM
#3
Thread Starter
Member
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"
]
-
Jul 17th, 2021, 02:12 PM
#4
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 19th, 2021, 12:58 PM
#5
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))
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|