I have extracted the UK Gov Bank Holidays using Delphi 10.1 with TRESTClient
and TRESTRequest
.
This is the results (cut down version):
{ "england-and-wales": {"division":"england-and-wales","events": [{"title":"New Year’s Day","date":"2018-01-01","notes":"","bunting":true}, {"title":"Good Friday","date":"2018-03-30","notes":"","bunting":false}, {"title":"Christmas Day","date":"2025-12-25","notes":"","bunting":true}, {"title":"Boxing Day","date":"2025-12-26","notes":"","bunting":true}]},
"scotland": {"division":"scotland","events": [{"title":"New Year’s Day","date":"2018-01-01","notes":"","bunting":true}, {"title":"2nd January","date":"2018-01-02","notes":"","bunting":true}, {"title":"Christmas Day","date":"2025-12-25","notes":"","bunting":true}, {"title":"Boxing Day","date":"2025-12-26","notes":"","bunting":true}]},
"northern-ireland": {"division":"northern-ireland","events": [{"title":"New Year’s Day","date":"2018-01-01","notes":"","bunting":true}, {"title":"St Patrick’s Day","date":"2018-03-19","notes":"Substitute day","bunting":true}, {"title":"Christmas Day","date":"2025-12-25","notes":"","bunting":true}, {"title":"Boxing Day","date":"2025-12-26","notes":"","bunting":true}]} }
Can anyone give an example how to iterate through the JSONValue
and display the contents to look something like:
england-and-wales
title=New Year’s Day, date=2018-01-01,notes=,bunting=true
title=Good Friday, date=2018-03-30,notes=,bunting=true
title=Christmas Day, date=2025-12-25,notes=,bunting=true
title=Boxing Day, date=2025-12-26,notes=,bunting=true
scotland
title=New Year’s Day, date=2018-01-01,notes=,bunting=true
title=2nd January, date=2018-01-02,notes=,bunting=true
title=Christmas Day, date=2025-12-25,notes=,bunting=true
title=Boxing Day, date=2025-12-26,notes=,bunting=true
northern-ireland
title=New Year’s Day, date=2018-01-01,notes=,bunting=true
title=St Patrick’s Day, date=2018-03-19,notes=,bunting=true
title=Christmas Day, date=2025-12-25,notes=,bunting=true
title=Boxing Day, date=2025-12-26,notes=,bunting=true
I would like to iterate through each division (country) and display the division name, then iterate through the events and display the Title, date, notes and bunting.
Hope that makes sense.
2
Answers
You can use the JSONToDelphiClass tool, which allows you to generate the necessary classes to read the JSON data in a simple way, from a JSON.
https://jsontodelphi.com/
Using this tool and the generated classes the code is greatly simplified.
You can do something like this:
To get this:
The
TRESTResponse.JSONValue
will be aTJSONValue
that points at aTJSONObject
instance.Cast that instance to
TJSONObject
, and then you can iterate through itsPairs[]
property, or use afor..in
loop on theTJSONObject
itself.For each pair, its
JsonString
will be the country/division name, and itsJsonValue
will be aTJSONValue
that points to aTJSONObject
instance.Cast each instance to
TJSONObject
and use itsValues[]
property to access thedivision
andevents
elements.Each
events
‘ value will be aTJSONValue
that points to aTJSONArray
instance. Cast the instance toTJSONArray
and then you can iterate itsItems[]
property, or use afor..in
loop on theTJSONArray
itself.For each item in the array, cast it to
TJSONObject
and iterate itsPairs[]
or use itsValues[]
to access thetitle
,date
, etc elements.So, for example: