Hopefully, this has not been covered elsewhere…I searched and could not find the info I needed.
I receive our database inquiries back as JSON. This is working fine. The issue, however, is when a particular call results in a successful processing of the request but does not return records.
Typical Example:
I initiate a call and receive a replay from the server. I parse the “details” and use the data. The return may look something like:
{
"Message":"Return",
"Details":[
{"amount":"50","flag_added_to_payroll":0,"flag_credit":0,"flag_debit":1,"pay_period_start":"2019-08-16 00:00:00","reason":"test"},
{"amount":"50","flag_added_to_payroll":0,"flag_credit":0,"flag_debit":1,"pay_period_start":"2019-08-01 00:00:00","reason":"test"},
{"amount":"50","flag_added_to_payroll":1,"flag_credit":0,"flag_debit":1,"pay_period_start":"2017-03-16 00:00:00","reason":"test"},
{"amount":"50","flag_added_to_payroll":1,"flag_credit":0,"flag_debit":1,"pay_period_start":"2017-03-01 00:00:00","reason":"test"}
]
}
An example of the problem case is:
{
"Message":"Return",
"Details":[]
}
This is happening when the server receives a call, processes it, and finds no records to return.
QUESTION: How do we identify this is the case in code? The following is an example of how we are parsing the returning JSON. I have played with several ways to determine if details[] is empty but keep getting an interesting assortment of errors. I’m hoping someone can shed some light on the matter for me! I am still fairly new to Xojo and make no claim to know anything with regard to “best practices” for this type of thing. If I’m going about this entirely wrong, please get me headed down the path I need to go.
[code]// Parse the JSON result
Dim aString As String = Response.DefineEncoding(Encodings.UTF8)
Dim d As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(aString.ToText)
Dim results() As Auto = d.Lookup(“Details”, “”)
For Each result As Xojo.Core.Dictionary In results
// THIS IS WHERE I WANT TO CHECK RESULTS() FOR BEING EMPTY
Dim period, amount, reason, creditT, debitT, procT As String
Dim credit, debit, proc As Integer
If result.HasKey(“pay_period_start”) Then
period = result.value (“pay_period_start”)
End If
If result.HasKey(“flag_credit”) Then
credit = result.value(“flag_credit”)
creditT=Str(credit)
End If
If result.HasKey(“flag_debit”) Then
debit = result.value(“flag_debit”)
debitT= Str(debit)
End If
If result.HasKey(“flag_added_to_payroll”) Then
proc = result.value (“flag_added_to_payroll”)
procT= Str(proc)
End If
If result.HasKey(“amount”) Then
amount = result.value(“amount”)
End If
If result.HasKey(“reason”) Then
reason = result.value(“reason”)
End If
pmod_listbox.AddRow(period, creditT, debitT, procT, amount, reason)
Next
[/code]
Thanks for your help!
Bill