【问题标题】:JSON object- how to iterate through all properties without knowing their names?JSON 对象 - 如何在不知道其名称的情况下遍历所有属性?
【发布时间】:2021-01-27 13:55:26
【问题描述】:

我有一个相当简单的 JSON:

[
    {
      "hero-img": "girl-ipad.png",
      "main-story-headline": "How to Stay Mentally Healthy During a Pandemic",
      "main-story-paragraph": "lorem ipsum",
      "main-story-button-text": "Read More"
    },
    {
      "hero-img": "painter-woman.png",
      "main-story-headline": "How to Stay Mentally Healthy During a Pandemic",
      "main-story-paragraph": "lorem ipsum",
      "main-story-button-text": "Explore More"
    },
    {
      "hero-img": "old-man.png",
      "main-story-headline": "How to Stay Mentally Healthy During a Pandemic",
      "main-story-paragraph": "lorem ipsum",
      "main-story-button-text": "Get Yours Now"
    } ]

通过使用 Delphi 10.3 Rio,我想遍历所有 属性,无论它们如何命名。我是这样开始的:

program JuTemplates;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, System.JSON, System.IOUtils, System.Types;

var
  root, OutputDir, TemplatesDir, JsonDir, jsonText: string;
  JsonFiles: TStringDynArray;
  i, j: Integer;
  JSONValue: TJSONObject;
  JSONValues: TJSONArray;
begin
  try

    try
      root := ExtractFilePath(ParamStr(0));
      OutputDir := root + 'OutputDir\';
      TemplatesDir := root + 'TemplatesDir\';
      JsonDir := root + 'JsonDir\';
      writeln('Processing: ' + JsonDir);
      JsonFiles := TDirectory.GetFiles(JsonDir);
      for i := 0  to High(JsonFiles) do
      begin
        jsonText := TFILE.ReadAllText(JsonFiles[i]);
        JSONValues := TJSONObject.ParseJSONValue(jsonText) as TJSONArray;

        for j := 0 to JSONValues.Count - 1 do
        begin
          JSONValue := JSONValues.Items[i] as TJSONObject;
          // here I should iterate through that JSONValue object
        end;
      end;
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  finally
    writeln ('Press any key to continue');
    readln;
  end;
end.

【问题讨论】:

    标签: json delphi delphi-10.3-rio


    【解决方案1】:

    要枚举 JSONObject,您可以使用如下枚举器。

    使用 for..in 循环(隐式枚举器)

    procedure TForm1.Button1Click(Sender: TObject);
    var
       JSONData       : String;
       JSONObject     : TJSONObject;
       JSONPair       : TJSONPair;
    begin
       JSONData   := '... some JSON data ...';   // Place you data here
       JSONObject := TJSonObject.ParseJSONValue(JSONData) as TJSONObject;
       try
         for JSONPair in JSONObject do
             ShowMessage(Format('1) Key=%s Value=%s',
                         [JSONPair.JsonString.ToString,
                          JSONPair.JsonValue.ToString]));
       finally
         JSONObject.Free;
       end;
    end;
    

    使用经典的 for 循环:

    procedure TForm1.Button1Click(Sender: TObject);
    var
       JSONData       : String;
       JSONObject     : TJSONObject;
       JSONPair       : TJSONPair;
       I              : Integer;
    begin
       JSONData   := '... some JSON data ...';   // Place you data here
       JSONObject := TJSonObject.ParseJSONValue(JSONData) as TJSONObject;
       try
         for I := 0 to JSONObject.Count - 1 do begin
           ShowMessage(Format('Key=%s Value=%s',
                       [JSONObject.Pairs[I].JsonString.ToString,
                        JSONObject.Pairs[I].JsonValue.ToString]));
         end;
       finally
         JSONObject.Free;
       end;
    end;
    

    使用显式枚举器和while循环:

    procedure TForm1.Button1Click(Sender: TObject);
    var
       JSONData       : String;
       JSONObject     : TJSONObject;
       JSONEnumerator : TJSONObject.TEnumerator;
       JSONPair       : TJSONPair;
    begin
       JSONData   := '... some JSON data ...';   // Place you data here
       JSONObject := TJSonObject.ParseJSONValue(JSONData) as TJSONObject;
       try
         JSONEnumerator := JSONObject.GetEnumerator;
         try
           while JSONEnumerator.MoveNext do begin
             JSONPair := JSONEnumerator.Current;
             ShowMessage(Format('Key=%s Value=%s',
                         [JSONPair.JsonString.ToString,
                          JSONPair.JsonValue.ToString]));
           end;
         finally
           JSONEnumerator.Free;
         end;
       finally
         JSONObject.Free;
       end;
    end;
    

    请注意,您可能需要检查 JSONPair.JsonValue 是否有子节点,并使用另一个枚举器递归地枚举这些子节点。

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 1970-01-01
      • 2013-05-10
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      相关资源
      最近更新 更多