skip to Main Content

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


  1. 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:

    uses
      BaseClass;
    
    procedure TForm3.Button1Click(Sender: TObject);
    begin
      // Parse the JSON (in Memo1)
      var root: TRoot := TRoot.Create;
      root.AsJson := Memo1.Lines.Text;
    
      for var i:integer := 0 to (root.EnglandAndWales.Events.Count - 1) do
        memo2.Lines.Add('Title=' + root.EnglandAndWales.Events[i].Title + ' ' +
                        'Date=' + DateToStr(root.EnglandAndWales.Events[i].Date) + ' ' +
                        'Notes=' + root.EnglandAndWales.Events[i].Notes);
    end;
    

    To get this:

    enter image description here

    Login or Signup to reply.
  2. The TRESTResponse.JSONValue will be a TJSONValue that points at a TJSONObject instance.

    Cast that instance to TJSONObject, and then you can iterate through its Pairs[] property, or use a for..in loop on the TJSONObject itself.

    For each pair, its JsonString will be the country/division name, and its JsonValue will be a TJSONValue that points to a TJSONObject instance.

    Cast each instance to TJSONObject and use its Values[] property to access the division and events elements.

    Each events‘ value will be a TJSONValue that points to a TJSONArray instance. Cast the instance to TJSONArray and then you can iterate its Items[] property, or use a for..in loop on the TJSONArray itself.

    For each item in the array, cast it to TJSONObject and iterate its Pairs[] or use its Values[] to access the title, date, etc elements.

    So, for example:

    var
      responseObj, divisionObj, eventObj: TJSONObject;
      divisionPair, eventPair: TJSONPair;
      eventsArr: TJSONArray;
      eventItem: TJSONValue;
      list: TStringList;
      tempStr: string;
    begin
      ...
      list := TStringList.Create;
      try
        responseObj := RESTResponse1.JsonValue as TJSONObject;
        for divisionPair in responseObj do
        begin
          divisionObj := divisionPair.JsonValue as TJSONObject;
          if (list.Count > 0) then list.Add('');
          list.Add(divisionObj.Values['division'].Value);
          eventsArr := divisionObj.Values['events'] as TJSONArray;
          for eventItem in eventsArr do
          begin
            eventObj := eventItem as TJSONObject;
            tempStr := '';
            for eventPair in eventObj do
            begin
              if (tempStr <> '') then tempStr := tempStr + ', ';
              tempStr := tempStr + eventPair.JsonString.Value + '=' + eventPair.JsonValue.Value;
            end;
            list.Add(tempStr);
          end;
        end;
      finally
        // use list as needed...
      end;
    end;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search