delphi JSON对象-如何在不知道属性名称的情况下迭代所有属性?

7bsow1i6  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(165)

我有一个相当简单的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.
bqf10yzr

bqf10yzr1#

要枚举JSONObject,可以使用如下所示的枚举器。
在循环中使用for..(隐式枚举数)

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是否有查尔兹项,并使用另一个枚举器递归地枚举这些子项。

相关问题