2021年9月2日 星期四

Delphi DataSet和JSON互轉函式

 一、DataSet轉JSON

//1)資料集轉換為JSON字串:
//需USES System.JSON;
function DataSetToJson(ADataset: TDataSet): string;
var
  LRecord: string;
  LField: TField;
  i: integer;
begin
  Result := '';
  if (not ADataset.Active) or (ADataset.IsEmpty) then
    Exit;
  Result := '[';
  ADataset.DisableControls;
  ADataset.First;
  while not ADataset.Eof do
  begin
    for i := 0 to ADataset.FieldCount - 1 do
    begin
      LField := ADataset.Fields[i];
      if LRecord = '' then
        LRecord := '{"' + LField.FieldName + '":"' + LField.Text + '"'
      else
        LRecord := LRecord + ',"' + LField.FieldName + '":"' + LField.Text + '"';
      if i = ADataset.FieldCount - 1 then
      begin
        LRecord := LRecord + '}';
        if Result = '[' then
          Result := Result + LRecord
        else
          Result := Result + ',' + LRecord;
        LRecord := '';
      end;
    end;
    ADataset.Next;
  end;
  ADataset.EnableControls;
  Result := Result + ']';
end;

二、JSON轉DataSet

//2)JSON字串轉換為資料集:
procedure JsonToDataSet(AJson: string; ADataset: TDataSet);
var
  jDataSet: TJSONArray;
  jRecord: TJSONObject;
  i, j: Integer;
begin
  if (AJson = '') or (ADataset = nil) or (not ADataset.Active) then
    Exit;
  jDataSet := TJSONArray.Create;
  jDataSet := TJSONObject.ParseJSONValue(AJson, True) as TJSONArray;
  while not ADataset.Eof do
    ADataset.Delete;
  for i := 0 to jDataSet.Size - 1 do
  begin
    ADataset.Append;
    jRecord := jDataSet.Get(i) as TJSONObject;
    for j := 0 to ADataset.FieldCount - 1 do
    begin
      if ADataset.Fields[j].ReadOnly then
        Continue;
      ADataset.Fields[j].AsString := jRecord.GetValue(ADataset.Fields[j].FieldName).Value;
    end;
    ADataset.Post;
  end;
end;

轉貼至: https://www.itread01.com/content/1548927014.html,(因應語法及版本做適當的修正)


Json To ClientDataset

procedure JsonToClientDataset(AJson:String; AClientDataset:TClientDataset);
var
  jDataSet: TJSONArray;
  jRecord: TJSONObject;
  i, j: Integer;
  procedure CreateClientDataset;
  var k, iFieldCount:Integer;
    sFieldName:String;
  begin
    AClientDataset.Close;
    AClientDataset.Fields.Clear;
    AClientDataset.FieldDefs.Clear;
    jRecord := jDataSet.Get(0) as TJSONObject;
    iFieldCount := jRecord.Count; //欄位數
    for k := 0 to iFieldCount-1 do
    begin
      sFieldName := Trim(jRecord.Pairs[k].JsonString.Value);
      AClientDataset.FieldDefs.Add(sFieldName, ftString, 2000);
    end;
    AClientDataset.CreateDataSet;
  end;
begin
  if (AJson = '') or (AClientDataset = nil) then
    Exit;
  jDataSet := TJSONArray.Create;
  jDataSet := TJSONObject.ParseJSONValue(AJson, True) as TJSONArray;
  //
  CreateClientDataset;
  //
  for i := 0 to jDataSet.Size - 1 do
  begin
    AClientDataset.Append;
    jRecord := jDataSet.Get(i) as TJSONObject;
    for j := 0 to AClientDataset.FieldCount - 1 do
    begin
      AClientDataset.Fields[j].AsString := jRecord.GetValue(AClientDataset.Fields[j].FieldName).Value;
      jDataSet.Count;
    end;
    AClientDataset.Post;
  end;
end;

沒有留言:

張貼留言