提取JSON值到多个类别中的方法是什么?

huangapple go评论90阅读模式
英文:

Delphi 10.1 How extract a JSON value into multiple categories

问题

Sure, here's the translated code snippet you requested:

我已经使用Delphi 10.1和`TRESTClient`以及`TRESTRequest`提取了英国政府的[银行假期](https://www.api.gov.uk/gds/bank-holidays/#bank-holidays)。

以下是结果(精简版本):

```json
{ "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":"代替日","bunting":true}, {"title":"Christmas Day","date":"2025-12-25","notes":"","bunting":true}, {"title":"Boxing Day","date":"2025-12-26","notes":"","bunting":true}]} }

可以有人示例如何遍历JSONValue并显示内容,使其看起来像这样:

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

答案1

得分: 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.

使用JSONToDelphiClass工具,它允许您生成必要的类来以简单的方式从JSON中读取JSON数据。

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:

要获得如下结果:

提取JSON值到多个类别中的方法是什么?

英文:

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:

提取JSON值到多个类别中的方法是什么?

答案2

得分: 1

TRESTResponse.JSONValue将是一个指向TJSONObject实例的TJSONValue

将该实例强制转换为TJSONObject,然后可以遍历其Pairs[]属性,或者在TJSONObject本身上使用for..in循环

对于每一对,其JsonString将是国家/地区名称,其JsonValue将是指向TJSONObject实例的TJSONValue

将每个实例强制转换为TJSONObject,并使用其Values[]属性访问divisionevents元素。

每个events的值将是一个指向TJSONArray实例的TJSONValue。将该实例强制转换为TJSONArray,然后可以遍历其Items[]属性,或者在TJSONArray本身上使用for..in循环。

对于数组中的每个项,将其强制转换为TJSONObject,然后遍历其Pairs[]或使用其Values[]来访问titledate等元素。

所以,例如:

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
    // 根据需要使用 list...
  end;
end;
英文:

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 &gt; 0) then list.Add(&#39;&#39;);
      list.Add(divisionObj.Values[&#39;division&#39;].Value);
      eventsArr := divisionObj.Values[&#39;events&#39;] as TJSONArray;
      for eventItem in eventsArr do
      begin
        eventObj := eventItem as TJSONObject;
        tempStr := &#39;&#39;;
        for eventPair in eventObj do
        begin
          if (tempStr &lt;&gt; &#39;&#39;) then tempStr := tempStr + &#39;, &#39;;
          tempStr := tempStr + eventPair.JsonString.Value + &#39;=&#39; + eventPair.JsonValue.Value;
        end;
        list.Add(tempStr);
      end;
    end;
  finally
    // use list as needed...
  end;
end;

huangapple
  • 本文由 发表于 2023年5月10日 19:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定