firedac数据集数据序列为JSON
FIREDAC数据库引擎充分地考虑了跨平台和跨语言的支持。
因此,FIREDAC数据集可以序列为BINXMLJSON,三种格式。
firedac数据集数据序列为JSON字符串:
1 procedure TForm1.Button1Click(Sender: TObject); 2 begin 3 var ss: TStringStream := TStringStream.Create('', TEncoding.UTF8); 4 FDQuery1.SaveToStream(ss, sfJSON); 5 Memo1.Text := ss.DataString; 6 ss.Free; 7 end; 8
1 procedure TForm1.Button3Click(Sender: TObject); 2 var 3 memtable: TFDMemTable; 4 ms: TStringStream; 5 begin 6 memtable := FDMemTable2; 7 memtable.CachedUpdates := True; 8 if memtable.State in dsEditModes then 9 memtable.Post; 10 if memtable.ChangeCount = 0 then 11 Exit; 12 memtable.ResourceOptions.StoreItems := [siDelta, siMeta]; 13 ms := TStringStream.Create('', TEncoding.UTF8); 14 try 15 memtable.SaveToStream(ms, sfJSON); 16 Memo1.Text := ms.DataString; 17 finally 18 ms.Free; 19 memtable.ResourceOptions.StoreItems := [siDelta, siMeta, siData]; 20 end; 21 end;
流导入到FDMEMTable时 必须把流指向起始的位置
procedure TForm_JSON.B_1Click(Sender: TObject); var Astream, Astream2: TStringStream; aa, aa1: string; Js: TJsonSerializer; begin Astream := TStringStream.Create; FDQuery1.Open; FDQuery1.SaveToStream(Astream,sfjson); Astream.position := 0;// 这一句十分重要 在这里十分感谢咏南兄 // FDQuery1.SaveToStream(Astream, sfjson); FDMemTable1.Close; // FDMemTable1.LoadFromStream(Astream,sfXML); FDMemTable1.LoadFromStream(Astream,sfJSON); end;