Einzelnen Beitrag anzeigen

samso

Registriert seit: 29. Mär 2009
439 Beiträge
 
#3

AW: JSON - lesen eines JSON Elements

  Alt 17. Aug 2019, 21:00
Code:
[
  {
    "total": "9",
    "page": "9",
    "records": "99",
    "rows": [
      {
        "id": "62316",
        "titleId": "47243",
        "subject": [
          "000607",
          "000607_",
          "001727"
        ],
        "keyFeatures": [
          "AI",
          "URL"
        ]
      },
      {
        "id": "66",
        "titleId": "47243",
        "subject": [
          "000607",
          "000607_",
          "001727"
        ],
        "keyFeatures": [
          "KK"
        ]
      }
    ],
    "suggestion": "90"
  }
]
Mir ist Dein Quelltext zu anstrengend. Ich halte mich da eher an das Beispiel aus Stackoverflow.
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  DataBase: String;
  JsonArray: TJSONArray;
  ArrayElement: TJSonValue;
  RowValue: TJSonValue;
  RowItem: TJSonValue;
  keyFeatures: TJSonValue;
  FeatureItem: TJSonValue;
  FeatureList: TStringlist;
  Id: Integer;
  Index: Integer;
begin
  Memo1.Clear;
  DataBase :=
    '[{"total":"9","page":"9","records":"99","rows":[{"id":"62316","titleId":"47243","subject":'
    + '["000607","000607_","001727"],"keyFeatures":["AI","URL"]},{"id":"66","titleId":"47243","subject":'
    + '["000607","000607_","001727"],"keyFeatures":["KK"]}],"suggestion":"90"}]';

  JsonArray := TJSonObject.ParseJSONValue(DataBase) as TJSONArray;
  try
    Index := 1;
    for ArrayElement in JsonArray do
    begin
      RowValue := (ArrayElement as TJSonObject).GetValue('rows');
      if RowValue is TJSONArray
      then
      begin
        for RowItem in TJSONArray(RowValue) do
        begin
          RowItem.TryGetValue('id', Id);
          keyFeatures := (RowItem as TJSonObject).GetValue('keyFeatures');
          if keyFeatures is TJSONArray
          then
          begin
            FeatureList := TStringlist.Create;
            try
              for FeatureItem in TJSONArray(keyFeatures) do
                FeatureList.Add(FeatureItem.Value);
              Memo1.Lines.Add(Format('%d: %d KeyFeatures: %s', [Index, Id, FeatureList.CommaText]));
            finally
              FeatureList.Free;
            end;
          end;
          inc(Index);
        end;
      end;
    end;
  finally
    JsonArray.Free;
  end;
end;

Geändert von samso (17. Aug 2019 um 21:33 Uhr)
  Mit Zitat antworten Zitat